Re: XMLValidatingReader misses errors
- From: "Geoff" <Geoff.Pennington.ctr@xxxxxxxxxxxxx>
- Date: Thu, 16 Jun 2005 13:56:53 -0400
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" <someone@xxxxxxxxxxxxx> wrote in message
news:eIrPrjpcFHA.1448@xxxxxxxxxxxxxxxxxxxxxxx
> XmlValidatingReader should catch all errors that you mention below. please
> post the XML and XSD you are trying to validate.
>
>
> "Geoff" <Geoff.Pennington.ctr@xxxxxxxxxxxxx> wrote in message
> news:%23s3C0iocFHA.3328@xxxxxxxxxxxxxxxxxxxxxxx
>> 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.
>>
>>
>
>
.
- Follow-Ups:
- Re: XMLValidatingReader misses errors
- From: Zafar Abbas
- Re: XMLValidatingReader misses errors
- References:
- XMLValidatingReader misses errors
- From: Geoff
- Re: XMLValidatingReader misses errors
- From: Zafar Abbas
- XMLValidatingReader misses errors
- Prev by Date: Re: XSLT Development Tools?
- Next by Date: Re: XML Validation with xsd of a generic node
- Previous by thread: Re: XMLValidatingReader misses errors
- Next by thread: Re: XMLValidatingReader misses errors
- Index(es):
Relevant Pages
|