English 中文(简体)
C#的XMLReader - 多个子集合在XSD模式验证失败
原标题:C# XMLReader - Multiple child collections failing Validation with XSD schema
  • 时间:2010-02-05 06:34:45
  •  标签:
  • c#
  • xml
  • xsd

我有一份提供的XSD文件。我不了解XSD的知识,无法开始修改它。

<?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>

我有以下的XML。

<Dataset>
  <Person>
    <PayrollNumber>1234567</PayrollNumber>
    <Surname>Denson</Surname>
    <Name>John-Jaime-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>

NET文件验证器与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

我该如何修改我的XSD?

问题回答

是的,您的 XSD 目前定义数据集中仅有一个元素 Person 的序列。

您可以通过更改XSD中的此行来轻松更改:

    <xs:element name="Person">

到 (dào)

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

If no minOccurs and maxOccurs values are given, they default 到 (dào)1 - so you get a minimum and a maximum of exactly one Person inside your dataset.

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签