Groups | Blog | Home
all groups > dotnet xml > june 2005 >

dotnet xml : XMLValidatingReader misses errors



Zafar Abbas
6/16/2005 10:29:13 AM
XmlValidatingReader should catch all errors that you mention below. please
post the XML and XSD you are trying to validate.


[quoted text, click to view]

Geoff
6/16/2005 11:31:42 AM
I am using an XMLValidatingReader to validate an XML file received via a web
service. I want to verify that the incoming file matches the XML schema.
When testing the validation routine, the XMLValidatingReader correctly flags
mis-matched tags such as <abc>some content</xyz> but does not catch other
errors. For example, it doesn't catch tags that are not part of the schema,
doesn't catch missing tags where the schema has minoccurs="1", and doesn't
catch incorrect values when the schema has specified an enumeration.

Am I doing something wrong, or maybe I just misunderstand the purpose of
XMLValidatingReader ? Here is my code:

Public Shared Function ValidateXMLSchema(ByVal pXMLFile As String, ByVal
pSchemaPath As String) As String
Dim tr As XmlTextReader
Dim xsc As XmlSchemaCollection
Dim vr As XmlValidatingReader

Dim strReturn As String = ""

'Read the XML schema with the text reader and add it to a schema
collection
tr = New XmlTextReader(New StreamReader(pSchemaPath))
xsc = New XmlSchemaCollection
xsc.Add(Nothing, tr)

'Create a Validating Reader and add the XMLSchemaCollection.
vr = New XmlValidatingReader(pXMLFile, XmlNodeType.Document,
Nothing)
vr.Schemas.Add(xsc)
vr.ValidationType = ValidationType.Schema

Try
While vr.Read()
'Do not need to actually do anything in the Read loop...
End While
vr.Close()
Catch ex As XmlSchemaException
strReturn = ex.Message
Catch ex As XmlException
strReturn = ex.Message
Catch ex As Exception
strReturn = ex.Message
Finally
'No code here yet
End Try
If strReturn = "" Then
strReturn = "success"
End If

Return strReturn
End Function

Much obliged.

Geoff
6/16/2005 1:56:53 PM
Thanks for your interest. As you can see from the XML and XSD, I should get
an error on <Action> because "xxx" is not a valid value; there should be an
error because there is no <SubOrg> tag; and there should be an error
because <SubOrganization> is not part of the schema. As noted before, tags
like <abc></bcd> do generate an error, so I know something is happening.

Here is the XML:
strXML = "<Software>" & _
"<Action>xxx</Action>" & _
"<Organization></Organization>" & _
"<SubOrganization>DIOR</SubOrganization>" & _
"<SoftwareName>10</SoftwareName>" & _
"<Version>12</Version>" & _
"<Platform>12</Platform>" & _
"<ProductDescription></ProductDescription>" & _
"<ProductType></ProductType>" & _
"<Manufacturer>3</Manufacturer>" & _
"<DataSource></DataSource>" & _
"<Upgrades></Upgrades>" & _
"<Cost>0.0000</Cost>" & _
"<LicenseKey></LicenseKey>" & _
"<LicensePurchased>8</LicensePurchased>" & _
"<LicenseUsed>9</LicenseUsed>" & _
"<LicenseEmail1></LicenseEmail1>" & _
"<LicenseEmail2></LicenseEmail2>" & _
"<LicenseEmail3></LicenseEmail3>" & _
"<SWStart>1900-01-01T00:00:00</SWStart>" & _
"<SWExpire>1900-01-01T00:00:00</SWExpire>" & _
"<PhysicalMedia>" & _
"<MediaType>CompactFlash</MediaType>" & _
"<BarcodeNumber>44</BarcodeNumber>" & _
"<NumberOfDisks>44</NumberOfDisks>" & _
"<LicenseKey>44</LicenseKey>" & _
"<OfficeLocation>44</OfficeLocation>" & _
"<DataSource>44</DataSource>" & _
"</PhysicalMedia>" & _
"</Software>"

Here is the XSD:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="SoftwareSchema"
targetNamespace="http://tempuri.org/SoftwareSchema.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified"
xmlns="http://tempuri.org/SoftwareSchema.xsd"
xmlns:mstns="http://tempuri.org/SoftwareSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="SoftwareSchema" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Software">
<xs:complexType>
<xs:sequence>
<xs:element name="Action" minOccurs="1"
maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration
value="Add"></xs:enumeration>
<xs:enumeration
value="Delete"></xs:enumeration>
<xs:enumeration
value="Update"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Organization" type="xs:string"
minOccurs="1" />
<xs:element name="SubOrg" type="xs:string"
minOccurs="1" />
<xs:element name="SoftwareName" type="xs:string"
minOccurs="1" />
<xs:element name="Version" type="xs:string"
minOccurs="1" />
<xs:element name="Platform" type="xs:string"
minOccurs="1" />
<xs:element name="ProductDescription"
type="xs:string" minOccurs="0" />
<xs:element name="ProductType" type="xs:string"
minOccurs="0" />
<xs:element name="Manufacturer" type="xs:string"
minOccurs="0" />
<xs:element name="DataSource" type="xs:string"
minOccurs="0" />
<xs:element name="Upgrades" type="xs:string"
minOccurs="0" />
<xs:element name="Cost" type="xs:decimal"
minOccurs="0" />
<xs:element name="LicenseType" type="xs:string"
minOccurs="0" />
<xs:element name="LicenseKey" type="xs:string"
minOccurs="0" />
<xs:element name="LicensePurchased"
type="xs:integer" minOccurs="0" />
<xs:element name="LicenseUsed" type="xs:integer"
minOccurs="0" />
<xs:element name="LicenseEmail1"
type="xs:string" minOccurs="0" />
<xs:element name="LicenseEmail2"
type="xs:string" minOccurs="0" />
<xs:element name="LicenseEmail3"
type="xs:string" minOccurs="0" />
<xs:element name="SWStart" type="xs:date"
minOccurs="0" />
<xs:element name="SWExpire" type="xs:date"
minOccurs="0" />
<xs:element name="PhysicalMedia" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="MediaType"
type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="BarcodeNumber"
type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="NumberOfDisks"
type="xs:integer" minOccurs="0" maxOccurs="1" />
<xs:element name="LicenseKey"
type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="OfficeLocation"
type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="DataSource"
type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>



[quoted text, click to view]
Geoff
6/16/2005 2:43:37 PM
I just realized my XML file was missing a declaration upfront, so I modified
the first line of the string to
strXML = "<?xml version=""1.0"" encoding=""ISO-8859-1"" ?><Software>" & _

but validation still doesn't catch the errors.

[quoted text, click to view]
Geoff
6/17/2005 12:20:07 PM
Seems that I didn't understand the role of the ValidationEventHandler. I
thought I could catch validation problems with a Try/Catch block. When I
added the handler, I got a flood of errors. On to the next problem!

[quoted text, click to view]

Zafar Abbas
6/20/2005 9:55:32 AM
Your XML has a couple of issues:

1. You can not have <Software> as the document root as it is not a global
element in the schema, ONLY global elements can serve as document roots, in
this case you can only use <SoftwareSchema> as the document root.

2. Your XML is missing the namespace declaration on the root. The elements
in your XML must belong to the target namespace of the schema in order to
validate.

For both of the issues above, the modified XML is here:

<SoftwareSchema xmlns="http://tempuri.org/SoftwareSchema.xsd">
<Software >
<Action>xxx</Action>
<Organization></Organization>
<SubOrganization>DIOR</SubOrganization>
<SoftwareName>10</SoftwareName>
<Version>12</Version>
<Platform>12</Platform>
<ProductDescription></ProductDescription>
<ProductType></ProductType>
<Manufacturer>3</Manufacturer>
<DataSource></DataSource>
<Upgrades></Upgrades>
<Cost>0.0000</Cost>
<LicenseKey></LicenseKey>
<LicensePurchased>8</LicensePurchased>
<LicenseUsed>9</LicenseUsed>
<LicenseEmail1></LicenseEmail1>
<LicenseEmail2></LicenseEmail2>
<LicenseEmail3></LicenseEmail3>
<SWStart>1900-01-01T00:00:00</SWStart>
<SWExpire>1900-01-01T00:00:00</SWExpire>
<PhysicalMedia>
<MediaType>CompactFlash</MediaType>
<BarcodeNumber>44</BarcodeNumber>
<NumberOfDisks>44</NumberOfDisks>
<LicenseKey>44</LicenseKey>
<OfficeLocation>44</OfficeLocation>
<DataSource>44</DataSource>
</PhysicalMedia>
</Software>
</SoftwareSchema>


With this I get the following validation errors, which I think are what you
expect:

The 'http://tempuri.org/SoftwareSchema.xsd:Action' element has an invalid
value according to its data type. The element
'http://tempuri.org/SoftwareSchema.xsd:Software' has invalid child element
'http://tempuri.org/SoftwareSchema.xsd:SubOrganization'. Expected
'http://tempuri.org/SoftwareSchema.xsd:SubOrg'. The
'http://tempuri.org/SoftwareSchema.xsd:SubOrganization' element is not
declared. An error occurred at file:///C:/tempTest/temp/test.xml, (5, 2).
The 'http://tempuri.org/SoftwareSchema.xsd:SWStart' element has an invalid
value according to its data type.
'http://tempuri.org/SoftwareSchema.xsd:SWExpire' element has an invalid
value according to its data type.


Thanks,
Zafar


[quoted text, click to view]
AddThis Social Bookmark Button