Re: Problem with XMLValidatingReader

From: Zafar Abbas [MSFT] (zafara_at_microsoft.com)
Date: 07/29/04


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



Relevant Pages

  • Re: How to define a "anywhere" node in my XSD
    ... be added everywhere in the XML document. ... dealing with real-life text documents was sidelined in the mad rush ... but this can be eliminated by generating the XSD ...
    (comp.text.xml)
  • Re: Representing Biz Entity in XML: Use XMLSerializer or custom using XSD?
    ... I guess you could say the root of our problems is trying to marrying up between a complex .NET object that implements OOP and an XML document to be used by a Web Service. ... VB) that correspond to that XSD. ... > Of course the business entity would have to do the creation and field ... XmlSerializer does this for you, ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: There must be a better way!
    ... returns the data in <QueryResponse> - I've found the XSD that goes with ... apply to the XML Document). ... Sterling International Consulting Group ...
    (microsoft.public.sqlserver.xml)
  • Re: How to define a "anywhere" node in my XSD
    ... I would like to specify in my XSD that the "Documentation" element can ... be added everywhere in the XML document. ... How can i declare this in my XSD? ... What you are asking for is equivalent to an inclusion in SGML. ...
    (comp.text.xml)
  • Re: Problem with XMLValidatingReader
    ... This should not cause an error as the XML document is valid ... >> Detects most schema infractions, so I'll assume this is a correct ... >> Can anyone explain why and, preferably, offer a fix? ... >> TIA ...
    (microsoft.public.dotnet.xml)