blondebier
Programmer
Hi Guys,
I've written a little class that takes two parameters XMLtoValidate and XSD as strings. See here :
It doesn't work though... I've lifted some code that I use in another application, but reads the XSD from a file. That one works, this doesn't.
I though it might be an encoding issue... Any ideas?
I've written a little class that takes two parameters XMLtoValidate and XSD as strings. See here :
Code:
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Public Class ValidateXMLTest
Dim schemaError As String
' Validates XML against XSD - returns error if found
Public Function ValidateXML(ByVal XMLtoValidate As String, ByVal XSD As String) As String
Try
Dim xsdrdr As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(XSD))
' Load the schema into the schema object...
Dim clsSchema As System.Xml.Schema.XmlSchema = System.Xml.Schema.XmlSchema.Read(xsdrdr, Nothing)
' Configure the reader to use validation, and add the schema we just loaded...
Dim clsReaderSettings As New System.Xml.XmlReaderSettings()
clsReaderSettings.ValidationType = ValidationType.Schema
clsReaderSettings.Schemas.Add(clsSchema)
clsReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
' Read the XML into an XmlReader...
Dim xmlrdr As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(XMLtoValidate))
' Create a reader that will read the document and validate it against the XSD...
Dim clsReader As System.Xml.XmlReader = System.Xml.XmlReader.Create(xmlrdr, clsReaderSettings)
' Validate the document...
Do While clsReader.Read()
Loop
clsReader.Close()
Return ""
Catch exXml As System.Xml.XmlException
schemaError = "Error on line " & exXml.LineNumber & ", "
schemaError += "Position " & exXml.LinePosition & ", "
schemaError += exXml.Message
Return schemaError
Catch exXsd As System.Xml.Schema.XmlSchemaException
schemaError = "Error on line " & exXsd.LineNumber & ", "
schemaError += "Position " & exXsd.LinePosition & ", "
schemaError += exXsd.Message
Return schemaError
Catch ex As Exception
'sqlP.Send("Schema validation exception - " & ex.Message.ToString)
Return schemaError
End Try
End Function
End Class
It doesn't work though... I've lifted some code that I use in another application, but reads the XSD from a file. That one works, this doesn't.
I though it might be an encoding issue... Any ideas?