开发者

C# XMLReader - Multiple child collections failing Validation with XSD schema

I have a supplied XSD. I do not know enough about XSD to begin modifying it.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Dataset">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Person">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="PayrollNumber" type="String16" maxOccurs="1" minOccurs="1" />
              <xs:element name="Surname" type="String50" maxOccurs="1" />
              <xs:element name="Name" type="String50" maxOccurs="1" />
              <xs:element name="StreetAddress" type="String50" maxOccurs="1" />
              <xs:element name="Suburb" type="String20" maxOccurs="1" />
              <xs:element name="Skills">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element maxOccurs="unbounded" name="Skill" type="String16" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="HomePhone" type="String14" maxOccurs="1" />
              <xs:element name="MobilePhone" type="String14" maxOccurs="1" />
              <xs:element name="PagerNumber" type="String14" maxOccurs="1" />
              <xs:element name="Email" type="String80" maxOccurs="1" />
              <xs:element name="RecordType" type="RecordType" maxOccurs="1" minOccurs="1" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="String16">
    <xs:restriction base="xs:string">
      <xs:maxLength value="16" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="String50">
    <xs:restriction base="xs:string">
      <xs:maxLength value="50" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="String20">
    <xs:restriction base="xs:string">
      <xs:maxLength value="20" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="String30">
    <xs:restriction base="xs:string">
      <xs:maxLength value="30" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="String14">
    <xs:restriction base="xs:string">
      <xs:maxLength value="14" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="String80">
    <xs:restriction base="xs:string">
      <xs:maxLength value="80" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="RecordType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="A" />
      <xs:enumeration value="E" />
      <xs:enumeration value="D" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

I have the following XML.

<Dataset>
  <Person>
    <PayrollNumber>1234567</PayrollNumber>
    <Surname>Denson</Surname>
    <Name>John-J开发者_开发知识库aime-Winston Junior</Name>
    <StreetAddress>Level 5, City Central Tower 2, 121 King William St</StreetAddress>
    <Suburb>Mitcham</Suburb>
    <Skills>
      <Skill>Skill1</Skill>
      <Skill>Skill2</Skill>
    </Skills>
    <HomePhone>08 8888 8888</HomePhone>
    <MobilePhone>041 888 999</MobilePhone>
    <PagerNumber>111111</PagerNumber>
    <Email>curly@stooge.com</Email>
    <RecordType>A</RecordType>
  </Person>
</Dataset>

The NET file validator works fine with XMLReader.

However if I introduce multiple Person records - ie. a collection the validation fails with Validation error: The element 'Dataset' has invalid child element 'Person'. 0 0

How can I modify my XSD?


Yes, your XSD right now defines a sequence of exactly one element Person inside the data set.

You can easily change that by changing this line in your XSD:

    <xs:element name="Person">

to

    <xs:element name="Person" minOccurs="1" maxOccurs="unbounded">

If no minOccurs and maxOccurs values are given, they default to 1 - so you get a minimum and a maximum of exactly one Person inside your dataset.

Change those values to something that makes sense to you, e.g. minOccurs="1", maxOccurs="10" or use the maxOccurs="unbounded" for any number of occurences (no limit).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜