2014-04-23 2 views
2

xsd :
HTML 테이블과 매우 유사합니다. 행에는 열과 열에 요소가 있습니다.C# 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)); 

<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이다)를 생성 예외 :

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

내 xsd 또는 C# 코드가 잘못 되었나요? (XML은 좋은 예입니다)? 내가 lablelabel과 동일하지 않습니다 생각

<lable>dddddd</lable> 

:

답변

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

여기 당신의 실수.

추가 : 제가 틀렸을 수도 있지만 적절한 순서를 유지하기로되어 있지 않습니까? 텍스트 상자가 있고 xsd의 첫 번째 열이 레이블이어야합니다.

+1

그 전에는 오류가 있습니다.하지만 문제는 시퀀스에서 사용 가능한 모든 요소가 필요하다는 것입니다. 내 대답을 참조하십시오. –

+2

이것이 문제인지 여부는 좋은 것으로 보입니다. –

+1

@EricScherrer 그렇습니다.하지만 XML에서 해당 줄 앞에 문제가 발생하면 OP가 게시하는 오류와 아무런 관련이 없습니다. –

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 표시기 장을 참조하십시오.