2014-04-23 68 views
2

我定义了一个xsd:
与HTML表非常相似。行有列和列有元素。C#xml验证

<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就是很好的例子)?

回答

6

该问题看起来与XSD。您所指定的col元素内的序列,它的预期col看起来像这样:

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

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

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

或使用<xs:choice>

+1

我认为,而不是xsl:选择你的意思是xsd:choice ... –

+0

@PetruGardea好点,我已经纠正它。 –

2

这是你的错误:

<lable>dddddd</lable> 

我觉得lable是不一样的label

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

+1

它在这之前是失败的(这也是一个错误),但问题是序列期待所有可用的元素 - 请参阅我的回答 –

+2

这是否是问题,看起来像一个很好的捕获。 –

+1

@EricScherrer它是,但它与OP发布的错误无关,因为问题发生在XML中的那一行之前。 –

0

的原因似乎是,当您定义了以下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

。在“雇员”元件可以直接通过命名元件,像这样被声明:

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

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