Dnx_7
asked on
Validate xml with an XSD file
Hi experts
i'm looking on how to validate a string xml (not a xml file) with an XSD file (physical file on drive)
i saw many code on internet but i haven't found any example of loading xml string and then validate it with a xsd file
please help
regards
i'm looking on how to validate a string xml (not a xml file) with an XSD file (physical file on drive)
i saw many code on internet but i haven't found any example of loading xml string and then validate it with a xsd file
please help
regards
ASKER
thank you pockymaster
i use you the link above to create this class in vb.net 2005
Public Class XML
Private errorsCount As Int32 = 0
Private errorMsg As StringBuilder
Public Sub ValidationHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
With errorMsg
.Append(args.Message)
.Append(vbCrLf)
End With
errorsCount += 1
End Sub
Public Function validateXml(ByVal strXMLDoc As String, ByVal xsdFile As String) As Boolean
Dim tr As XmlTextReader = Nothing
Dim xss As XmlSchemaSet = Nothing
Dim xrs As XmlReaderSettings = Nothing
Dim xr As XmlReader
tr = New XmlTextReader(xsdFile)
xss = New XmlSchemaSet
xss.Add(Nothing, tr)
xrs = New XmlReaderSettings
xrs.Schemas.Add(xss)
xrs.ValidationType = ValidationType.Schema
AddHandler xrs.ValidationEventHandler , AddressOf ValidationHandler
errorMsg = New StringBuilder
errorMsg.Remove(0, errorMsg.Length)
xr = XmlReader.Create(strXMLDoc , xrs)
While xr.Read
End While
xr.Close()
If errorsCount > 0 Then
Return False
Else
Return True
End If
End Function
End Class
but it doesn't work, i don't know why because when the xml is correct, that's ok the class works fine
when i modify the root (make an error), the class works fine too
and when i modify the childrent, the class doesn't detect the error
can you help me?
regards
i use you the link above to create this class in vb.net 2005
Public Class XML
Private errorsCount As Int32 = 0
Private errorMsg As StringBuilder
Public Sub ValidationHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
With errorMsg
.Append(args.Message)
.Append(vbCrLf)
End With
errorsCount += 1
End Sub
Public Function validateXml(ByVal strXMLDoc As String, ByVal xsdFile As String) As Boolean
Dim tr As XmlTextReader = Nothing
Dim xss As XmlSchemaSet = Nothing
Dim xrs As XmlReaderSettings = Nothing
Dim xr As XmlReader
tr = New XmlTextReader(xsdFile)
xss = New XmlSchemaSet
xss.Add(Nothing, tr)
xrs = New XmlReaderSettings
xrs.Schemas.Add(xss)
xrs.ValidationType = ValidationType.Schema
AddHandler xrs.ValidationEventHandler
errorMsg = New StringBuilder
errorMsg.Remove(0, errorMsg.Length)
xr = XmlReader.Create(strXMLDoc
While xr.Read
End While
xr.Close()
If errorsCount > 0 Then
Return False
Else
Return True
End If
End Function
End Class
but it doesn't work, i don't know why because when the xml is correct, that's ok the class works fine
when i modify the root (make an error), the class works fine too
and when i modify the childrent, the class doesn't detect the error
can you help me?
regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that's strange...
does the validationHandler is called only once when it detect an error?
because when i make several error in the XML, the validationHandler is called only once and then the reader continue to read without calling validationHandler again even there are other error...
otherwise the class works fine but don't called the validation error as many times i put error in xml string.
here is the XSD maybe it will help you to make me understand.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="ROOTTYPE">
<xsd:sequence>
<xsd:element name="ID" type="xsd:string"/>
<xsd:element name="VALIDITY" type="xsd:string"/>
<xsd:element name="TYPE" type="xsd:string"/>
<xsd:element name="TXT" type="xsd:string"/>
<xsd:element name="IMG" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ROOT" type="ROOTTYPE"/>
</xsd:schema>
regards
does the validationHandler is called only once when it detect an error?
because when i make several error in the XML, the validationHandler is called only once and then the reader continue to read without calling validationHandler again even there are other error...
otherwise the class works fine but don't called the validation error as many times i put error in xml string.
here is the XSD maybe it will help you to make me understand.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="ROOTTYPE">
<xsd:sequence>
<xsd:element name="ID" type="xsd:string"/>
<xsd:element name="VALIDITY" type="xsd:string"/>
<xsd:element name="TYPE" type="xsd:string"/>
<xsd:element name="TXT" type="xsd:string"/>
<xsd:element name="IMG" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ROOT" type="ROOTTYPE"/>
</xsd:schema>
regards
If I create invalid attributes, it will go through the validationHandler for each attribute
If i create invalid members it returns only the last wrong element... hmm.. maybe I'm missing something as well
If i create invalid members it returns only the last wrong element... hmm.. maybe I'm missing something as well
ASKER
thank you, i'll manage with that for now :)
http://www.codeguru.com/Csharp/Csharp/cs_data/xml/article.php/c6737/
and instead of passing a filename to the validator, pass a string containing your xml.
so:
vr = new XmlValidatingReader( strXMLDoc,
XmlNodeType.Document, null);
will be
vr = new XmlValidatingReader( strYourXMLDataString,
XmlNodeType.Document, null);