简体   繁体   English

C#xml验证

[英]C# xml validation

I defined a xsd: 我定义了一个xsd:
Very similar to HTML table. 与HTML表格非常相似。 rows has columns and columns has elements. 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>

And created the following xml (for me is a valid xml): 并创建了以下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>

When I'm trying to validate that: 当我试图验证:

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

I'm getting the following exception: 我收到以下异常:

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

Whats wrong with my xsd or C# code ? 我的xsd或C#代码有什么问题? (the xml is good example) ? (xml是很好的例子)?

The problem looks to be with the XSD. 问题似乎与XSD有关。 You have specified a sequence within the col element and it's expecting col to look like this: 您已在col元素中指定了一个序列,并且它期望col看起来像这样:

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

You either need to put minOccurs="0" on each element: 您需要在每个元素上放置minOccurs="0"

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

or use <xs:choice> 或使用<xs:choice>

Here's your mistake: 这是你的错:

<lable>dddddd</lable>

I think lable is not the same as label . 我认为lablelabel不一样。

Additionally: I may be wrong, but aren't you supposed to keep the proper order? 另外:我可能错了,但你不应该保持正确的秩序吗? You've got a textbox, while your first column in xsd is supposed to be a label. 你有一个文本框,而xsd中的第一列应该是一个标签。

The reason appears to be that you have the incorrect order in your XML example when you have defined the following XSD: 原因似乎是您在定义以下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>

I am not an XSD expert but I was curious about your question and read the following on W3Schools.com. 我不是XSD专家,但我对您的问题感到好奇并在W3Schools.com上阅读以下内容。 I hope I am reading this correctly, I just wanted to show what I have found researching your question. 我希望我正确地读到这个,我只想表明我发现了研究你的问题。

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

. The "employee" element can be declared directly by naming the element, like this: 可以通过命名元素直接声明“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>

If you use the method described above, only the "employee" element can use the specified complex type. 如果使用上述方法,则只有“employee”元素可以使用指定的复杂类型。 Note that the child elements, "firstname" and "lastname", are surrounded by the indicator. 请注意,子元素“firstname”和“lastname”被指示符包围。 This means that the child elements must appear in the same order as they are declared. 这意味着子元素必须以与声明它们相同的顺序出现。 You will learn more about indicators in the XSD Indicators chapter. 您将在XSD指标章节中了解有关指标的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM