Re: Problem with XMLValidatingReader
From: Zafar Abbas [MSFT] (zafara_at_microsoft.com)
Date: 07/29/04
- Next message: Matt_T: "Getting "Visual Studio could not locate a schema for this document"
- Previous message: Mike Morse: "Re: Namespaces ?"
- In reply to: Budman: "Problem with XMLValidatingReader"
- Next in thread: Budman: "Re: Problem with XMLValidatingReader"
- Reply: Budman: "Re: Problem with XMLValidatingReader"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 29 Jul 2004 13:03:55 -0700
Your test harness code is correct. But your expectation from the XSD and XML
is incorrect. This should not cause an error as the XML document is valid
according to the schema.
All of the element particles under the xs:choice have minOccurs="0" which
make the effective total range of the <choice> particle 0 [1]. So it can be
eliminated from the XML Document.
[1] : http://www.w3.org/TR/xmlschema-1/#cos-choice-range
"Budman" <Budman@discussions.microsoft.com> wrote in message
news:D6BBEE27-B672-4FA9-A4FB-57B76C35C453@microsoft.com...
> Using the following as a test harness:
>
> string xmlDataDocName = "test.xml";
> string xmlSchemaDocName = "test.xsd";
>
>
> XmlValidatingReader xmlReader = new XmlValidatingReader(new
XmlTextReader(xmlDataDocName));
> xmlReader.Schemas.Add(null, new XmlTextReader(xmlSchemaDocName));
> xmlReader.ValidationType = ValidationType.Schema;
>
> XmlDocument xmlDoc = new XmlDocument();
> try
> {
> xmlDoc.Load(xmlReader); // if this throws an error, doc is invalid
> txtBox.Text = xmlDoc.OuterXml;
> }
> catch (XmlSchemaException ex)
> {
> string errMsg = "Schema Validation Error\n" +
> " Line Number : " + ex.LineNumber + "\n" +
> " Line Position : " + ex.LinePosition + "\n" +
> " Message : " + ex.Message + "\n";
>
> txtBox.Text = errMsg;
> }
> catch (XmlException ex)
> {
> txtBox.Text = "XML Exception: " + ex.Message;
> }
> catch (Exception ex)
> {
> txtBox.Text = "General Exception: " + ex.Message;
> }
>
> Detects most schema infractions, so I'll assume this is a correct
approach. However, it does not handle the following:
>
> xsd:
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema targetNamespace="urn:test"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:test"
elementFormDefault="qualified" attributeFormDefault="unqualified">
> <xs:annotation>
> <xs:documentation xml:lang="en">
> The idea behind the group reference was to be able to have all of the
"B's" be optional while, at the same time, requiring at least one of them to
be present. </xs:documentation>
> </xs:annotation>
> <xs:element name="Root">
> <xs:complexType mixed="true">
> <xs:sequence>
> <xs:element name="A" type="xs:string"/>
> <xs:group ref="B" maxOccurs="5"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:group name="B">
> <xs:choice>
> <xs:element name="B1" type="xs:string" minOccurs="0"/>
> <xs:element name="B2" type="xs:string" minOccurs="0"/>
> <xs:element name="B3" type="xs:string" minOccurs="0"/>
> <xs:element name="B4" type="xs:string" minOccurs="0"/>
> <xs:element name="B5" type="xs:string" minOccurs="0"/>
> </xs:choice>
> </xs:group>
> </xs:schema>
>
> Instance doc:
> <?xml version="1.0" encoding="UTF-8"?>
> <Root xmlns="urn:test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:test test.xsd">
> <A/>
> <!--
> <B1>1</B1>
> <B2>2</B2>
> <B3>3</B3>
> <B4>4</B4>
> <B5>5</B5>
> <B5>5</B5>
> <B5>5</B5>
> <B5>5</B5>
> -->
> </Root>
>
> Can anyone explain why and, preferably, offer a fix?
>
> TIA
- Next message: Matt_T: "Getting "Visual Studio could not locate a schema for this document"
- Previous message: Mike Morse: "Re: Namespaces ?"
- In reply to: Budman: "Problem with XMLValidatingReader"
- Next in thread: Budman: "Re: Problem with XMLValidatingReader"
- Reply: Budman: "Re: Problem with XMLValidatingReader"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|