2

I defined a xsd:
Very similar to HTML table. rows has columns and columns has elements.

<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 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 ? (the xml is good example) ?

3 Answers 3

6

The problem looks to be with the XSD. You have specified a sequence within the col element and it's expecting col to look like this:

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

You either need to put minOccurs="0" on each element:

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

or use <xs:choice>

Sign up to request clarification or add additional context in comments.

Comments

2

Here's your mistake:

<lable>dddddd</lable>

I think lable is not the same as label.

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.

2 Comments

It's failing before that (it is also a fault) but the problem is that the sequence is expecting all available elements - see my answer
@EricScherrer it is, but it has nothing to do with the error the OP is posting as the problem occurs before that line in the XML.
0

The reason appears to be that you have the incorrect order in your XML example when you have defined the following XSD:

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

. The "employee" element can be declared directly by naming the element, like this:

<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. Note that the child elements, "firstname" and "lastname", are surrounded by the indicator. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.