【问题标题】:C# xml validationC# xml 验证
【发布时间】:2014-06-08 11:34:25
【问题描述】:

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

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

并创建了以下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>

当我尝试验证时:

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));

我收到以下异常:

The element 'col' has invalid child element 'textbox'. List of possible elements expected: 'label'

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

【问题讨论】:

    标签: c# xml xsd xsd-validation


    【解决方案1】:

    这是你的错误:

    <lable>dddddd</lable>
    

    我认为lablelabel 不一样。

    另外:我可能是错的,但你不应该保持正确的顺序吗?你有一个文本框,而你在 xsd 中的第一列应该是一个标签。

    【讨论】:

    • 在此之前它失败了(这也是一个错误),但问题是该序列需要所有可用元素 - 请参阅我的答案
    • @EricScherrer 它是,但它与 OP 发布的错误无关,因为问题发生在 XML 中的该行之前。
    【解决方案2】:

    问题似乎出在 XSD 上。您在 col 元素中指定了一个序列,它期望 col 看起来像这样:

    <col>
        <label />
        <image />
        <textbox />
        <yesno />
        <calendar />
        <select />
    </col>
    

    您需要将minOccurs="0" 放在每个元素上:

    <xs:element name="label" type="xs:string" minOccurs="0" />
    

    或使用&lt;xs:choice&gt;

    【讨论】:

      【解决方案3】:

      原因似乎是当您定义了以下 XSD 时,您的 XML 示例中的顺序不正确:

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

      我不是 XSD 专家,但我对您的问题很好奇,并在 W3Schools.com 上阅读了以下内容。我希望我没看错,我只是想展示我在研究你的问题时发现了什么。

      http://www.w3schools.com/Schema/schema_complex.asp

      。 “employee”元素可以通过命名元素直接声明,如下所示:

      <xs:element name="employee">
        <xs:complexType>
          <xs:sequence>
           <xs:element name="firstname" type="xs:string"/>
            <xs:element name="lastname" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      

      如果使用上述方法,则只有“employee”元素可以使用指定的复杂类型。请注意,子元素“firstname”和“lastname”被指示符包围。这意味着子元素必须以与声明它们相同的顺序出现。您将在 XSD 指标一章中了解有关指标的更多信息。

      【讨论】:

        猜你喜欢
        • 2013-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-01
        • 2022-11-09
        • 1970-01-01
        • 1970-01-01
        • 2011-03-23
        相关资源
        最近更新 更多