C#xml验证

Nic*_*ckF 2 c# xml xsd xsd-validation

我定义了一个xsd:
非常类似于HTML表.rows有列,列有元素.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:attributeGroup name="basics">
    <xs:attribute name="title" type="xs:string" use="required"/>
    <xs:attribute name="field_id" type="xs:string" use="required"/>
    <xs:attribute name="is_mandatory" type="xs:boolean" use="required"/>
  </xs:attributeGroup>

  <xs:element name="form">
    <xs:complexType>      
      <xs:sequence>
        <xs:element name="row">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="col">
                <xs:complexType>
                  <xs:sequence>
                    <!-- lable -->
                    <xs:element name="label" type="xs:string"/>

                    <!-- image -->
                    <xs:element name="image" >
                      <xs:complexType>
                        <xs:attribute name="src" type="xs:string" use="required"/>
                      </xs:complexType>
                    </xs:element>

                    <!-- textbox -->
                    <xs:element name="textbox">
                      <xs:complexType>
                        <xs:attributeGroup ref="basics"/>
                        <xs:attribute name="hint" type="xs:string" use="optional" default=""/>
                      </xs:complexType>
                    </xs:element>

                    <!-- yesno -->
                    <xs:element name="yesno">
                      <xs:complexType>
                        <xs:attributeGroup ref="basics"/>                        
                      </xs:complexType>
                    </xs:element>

                    <!-- calendar -->
                    <xs:element name="calendar">
                      <xs:complexType>
                        <xs:attributeGroup ref="basics"/>
                      </xs:complexType>
                    </xs:element>

                    <!-- Select/ multi select -->
                    <xs:element name="select">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="option"/>
                        </xs:sequence>
                        <xs:attributeGroup ref="basics"/>
                        <xs:attribute name="singleChoice" type="xs:boolean" use="required"/>
                      </xs:complexType>
                    </xs:element>

                  </xs:sequence>                  
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="title" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

并创建了以下xml(对我来说是一个有效的xml):

<?xml version="1.0" encoding="utf-8" ?>
<form title="title">
    <row>
        <col>
            <textbox title="aaa" field="Name" mandatory="false" hint="aaa"/>
        </col> 
    </row>

    <row>
        <col>
            <textbox title="bbb" field="UID" mandatory="true" hint="bbb"/>
            <yesno title="dddddd" field="field-yesno" mandatory="true"/>
        </col> 
    </row>

    <row>
        <col>
            <lable>dddddd</lable>
        </col>
        </row>

    <row>
        <col>
            <calendar title="cccc" field="StartDate" mandatory="true"/>
        </col>
    </row>

    <row>
        <col>
            <select title="select" field="ffff" mandatory="true">
                <option value="1">option 1</option>
                <option value="2" selected="true">option 2</option>
                <option value="3">option 3</option>
            </select>
        </col>
    </row>
</form>
Run Code Online (Sandbox Code Playgroud)

当我试图验证:

XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add(null, getAbsolutePath("xml\\form_schema.xsd"));
        settings.ValidationType = ValidationType.Schema;
        settings.CloseInput = true;
        settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings |
                                   XmlSchemaValidationFlags.ProcessIdentityConstraints |
                                   XmlSchemaValidationFlags.ProcessInlineSchema |
                                   XmlSchemaValidationFlags.ProcessSchemaLocation;

        // create xml reader
        XmlReader reader = XmlReader.Create(new StringReader(xml), settings);
        XmlDocument document = new XmlDocument();
        document.Load(reader);
        document.Validate(new ValidationEventHandler(ValidationHandler));
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

The element 'col' has invalid child element 'textbox'. List of possible elements expected: 'label'
Run Code Online (Sandbox Code Playgroud)

我的xsd或C#代码有什么问题?(xml是很好的例子)?

Tre*_*ley 6

问题似乎与XSD有关.您已在col元素中指定了一个序列,并且它col看起来像这样:

<col>
    <label />
    <image />
    <textbox />
    <yesno />
    <calendar />
    <select />
</col>
Run Code Online (Sandbox Code Playgroud)

你要么需要穿上minOccurs="0"每个元素:

<xs:element name="label" type="xs:string" minOccurs="0" />
Run Code Online (Sandbox Code Playgroud)

或使用 <xs:choice>