473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

XMLValidatingReader misses errors

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.
Nov 12 '05 #1
5 1383
XmlValidatingReader should catch all errors that you mention below. please
post the XML and XSD you are trying to validate.
"Geoff" <Ge******************@NOSPAMwhs.mil> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

Nov 12 '05 #2
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>

"Zafar Abbas" <so*****@somewhere.com> wrote in message
news:eI**************@TK2MSFTNGP14.phx.gbl...
XmlValidatingReader should catch all errors that you mention below. please
post the XML and XSD you are trying to validate.
"Geoff" <Ge******************@NOSPAMwhs.mil> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.


Nov 12 '05 #3
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.

"Geoff" <Ge******************@NOSPAMwhs.mil> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
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>

"Zafar Abbas" <so*****@somewhere.com> wrote in message
news:eI**************@TK2MSFTNGP14.phx.gbl...
XmlValidatingReader should catch all errors that you mention below.
please
post the XML and XSD you are trying to validate.
"Geoff" <Ge******************@NOSPAMwhs.mil> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.



Nov 12 '05 #4
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!

"Geoff" <Ge******************@NOSPAMwhs.mil> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

Nov 12 '05 #5
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
"Geoff" <Ge******************@NOSPAMwhs.mil> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
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>

"Zafar Abbas" <so*****@somewhere.com> wrote in message
news:eI**************@TK2MSFTNGP14.phx.gbl...
XmlValidatingReader should catch all errors that you mention below. please post the XML and XSD you are trying to validate.
"Geoff" <Ge******************@NOSPAMwhs.mil> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.



Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MT | last post by:
Hi, I am currently validating an XML file against a Schema using XMLValidatingReader. The schema actually contains ranges for particular elements and I have been using it to detect range errors...
2
by: Ed Bacon | last post by:
I am a bit puzzled about the differences between XmlTextReader and XmlValidatingReader. I have been able to successfully load a DataSet object from and XML file via the text reader. However,...
1
by: Dmitry Martynov | last post by:
Hi I have a question whether XmlValidatingReader doesn't check keyref constrain (the same with key constraint) or I do smth wrong. I have the following schema <?xml version="1.0"...
3
by: Nuevo Registrado | last post by:
All, I am having a tough time figuring out how to code a simple app to read an xml file and an xsd file and validate the xml file using the xsd without using a namespace for the schema. Help?...
9
by: jason | last post by:
how do you use the XmlValidatingReader to validate an XML document that is passed into the XmlValidatingReader constructor? it looks like the normal process is to use an underlying reader, as...
1
by: Bernhard Felkel | last post by:
I have troubles validating XML files with key/keyref constraints. HereĀ“s my schema: <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"...
1
by: Wolfgang Uhr | last post by:
Hello I have the following problem. I want to use the validating parser XmlValidatingReader and so I write ... FileStream stream = new FileStream(ofd.FileName, FileMode.Open);...
1
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST> xml file 2
12
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.