Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating XML fragment against schema

Status
Not open for further replies.

teclioness

Programmer
Jul 12, 2000
5
GB
Hi,

I am finding diffcult to validate a xml fragment to validate against a schema.
The situation is as follows:

I am making an xml string on the fly, but before submitting it to another server, I need to validate that it is a valid xml against schema. I have the schema saved locally on the server. Here is the xml:

<?xml version='1.0' encoding='UTF-8'?>
<portalReq xmlns='<requestID>1234</requestID>
<language>en</language>
<source>l</source>
<returnURL><backURL><cart>
<items><item itemNumber='1'>
<reference>CTax123</reference>
<fundCode>05</fundCode>
<accountName>Mr. A J Kaul</accountName>
<amount>112.56</amount>
</item>
</items>
</cart>
</portalReq>

Following is the schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns=" xmlns:xs=" targetNamespace=" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="portalReq">
<xs:complexType>
<xs:sequence>
<xs:element ref="requestID"/>
<xs:element ref="userID" minOccurs="0"/>
<xs:element ref="language"/>
<xs:element ref="source"/>
<xs:element ref="returnURL"/>
<xs:element ref="backURL"/>
<xs:element name="cart">
<xs:complexType>
<xs:sequence>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="reference"/>
<xs:element ref="reference2" minOccurs="0"/>
<xs:element ref="fundCode"/>
<xs:element ref="accountName"/>
<xs:element name="amount" type="xs:decimal"/>
<xs:element name="eMails" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="eMail" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="vatInfo" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="vatCode"/>
<xs:element ref="vatRate" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="address" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="address1"/>
<xs:element ref="address2" minOccurs="0"/>
<xs:element ref="address3" minOccurs="0"/>
<xs:element ref="address4" minOccurs="0"/>
<xs:element ref="postCode"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="additionalInfo" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="userString1" minOccurs="0"/>
<xs:element ref="userString2" minOccurs="0"/>
<xs:element ref="userString3" minOccurs="0"/>
<xs:element ref="userString4" minOccurs="0"/>
<xs:element ref="userString5" minOccurs="0"/>
<xs:element ref="userLong1" minOccurs="0"/>
<xs:element ref="userLong2" minOccurs="0"/>
<xs:element ref="userLong3" minOccurs="0"/>
<xs:element ref="userLong4" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="itemNumber" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="requestID" type="xs:int"/>
<xs:element name="userID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="language">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="source">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="1"/>
<xs:enumeration value="l"/>
<xs:enumeration value="M"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="returnURL" type="xs:string"/>
<xs:element name="backURL" type="xs:string"/>
<!--<xs:element name="eMails">
<xs:complexType>
<xs:sequence>
<xs:element ref="eMail" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>-->
<xs:element name="postCode">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="reference">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="reference2">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="userLong1" type="xs:int"/>
<xs:element name="userLong2" type="xs:int"/>
<xs:element name="userLong3" type="xs:int"/>
<xs:element name="userLong4" type="xs:int"/>
<xs:element name="userString1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="userString2">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="userString3">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="userString4">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="userString5">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="vatCode">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="vatRate" type="xs:decimal"/>
<xs:element name="fundCode">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="accountName">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="eMail">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="address1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="address2">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="address3">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="address4">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>

I have come across XmlValidatingReader class and tried to use it, but even if I take out one required element from xml, it does not produce error: Following is the code:

Private Function ValidateXML(ByVal objStr As StringReader)
Dim reader As XmlValidatingReader = Nothing
Dim myschema As New XmlSchemaCollection

Try
'Create the XML fragment to be parsed.
Dim xmlFrag As String = objStr.ReadToEnd

'Create the XmlParserContext.
Dim context As New XmlParserContext(Nothing, Nothing, "", XmlSpace.None)

'Implement the reader.
reader = New XmlValidatingReader(xmlFrag, XmlNodeType.Element, context)

'Add the schema.
Dim strXSD As String = Server.MapPath("abc.xsd")
myschema.Add(" strXSD)

'Set the schema type and add the schema to the reader.
reader.ValidationType = ValidationType.Schema
reader.Schemas.Add(myschema)

'Add the handler to raise the validation event.
AddHandler reader.ValidationEventHandler, AddressOf Me.ValidationEventHandle

While reader.Read
End While
Label1.Text = Label1.Text + "Completed validating xmlfragment"

Catch XmlExp As XmlException
Label1.Text = Label1.Text + XmlExp.Message
Catch XmlSchExp As XmlSchemaException
Label1.Text = Label1.Text + XmlSchExp.Message
Catch GenExp As Exception
Label1.Text = Label1.Text + GenExp.Message
End Try
End Function
Public Sub ValidationEventHandle(ByVal sender As Object, ByVal args As ValidationEventArgs)
m_success = False
Console.WriteLine((ControlChars.CrLf & ControlChars.Tab & "Validation error: " & args.Message))
End Sub 'ValidationEventHandle
Private Function validateXML(ByVal xml As String)
Try
'Is the xml string valid?
If (xml Is Nothing Or xml.Length < 1) Then
Return False
End If

Dim srXml As StringReader = New StringReader(xml)
Return validateXML(srXml)
Catch ex As Exception
ValidationError = ex.Message
Return False
End Try
End Function
Public Property ValidationError()
Get
Return "<ValidationError>" + ValidationError + "</VALIDATIONERROR>"
End Get
Set(ByVal Value)
ValidationError = Value
End Set
End Property
Public Sub validationCallBack(ByVal sender As Object, ByVal args As ValidationEventArgs)
Label1.Text = "Error:" + args.Message
End Sub


Please help!!
 
There are a ton of good examples on the internet, though finding them without an object name is sometimes a little bit of a pain :)

Try looking into the XmlValidatingReader object. This object will allow you to bind to a DTD, XSD, etc, load in an XML stream, then step through it. This gives you an additional advantage of being able to know not just that the file failed to validate, but details about where it failed. Always a plus.

Hope this helps,
-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top