1

I have an XML file looking like the one below. I am using it to give a series of commands to a robot:

<Task StartPosition="100,100">
      <GoTo>
        <X>100</X>
        <Y>200</Y>
      </GoTo>
      <MoveForward>
        <Distance>50</Distance><!--cm-->
      </MoveForward>
      <Rotate Direction="clockwise" Time="2">
        <Degrees>60</Degrees>
      </Rotate>
      <GoTo>
        <X>200</X>
        <Y>300</Y>
      </GoTo>
      <Rotate Direction="clockwise">
        <Degrees>120</Degrees>
      </Rotate>
      <SoundRecord>
        <Time>5</Time>
      </SoundRecord>
      <SoundPlayback>
        <Time>5</Time>
      </SoundPlayback>
    </Task>

As you can see the Task element has child elements of same type that are not placed one after another, like the GoTo element. I used the xsd.exe from the Microsoft Visual Studio Command Prompt (2010) to generate this schema based on the XML file above:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Task">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="GoTo" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="X" type="xs:string" minOccurs="0" />
              <xs:element name="Y" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="MoveForward" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Distance" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Rotate" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Degrees" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
            </xs:sequence>
            <xs:attribute name="Direction" type="xs:string" />
            <xs:attribute name="Time" type="xs:string" />
          </xs:complexType>
        </xs:element>
        <xs:element name="SoundRecord" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Time" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="SoundPlayback" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Time" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="StartPosition" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="Task" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

I am validating my XML files, including the one in this example against this schema using the following code:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, xsdFilePath);
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(xmlFilePath, settings);

// Parse the file to validate it
while (reader.Read());

\**************************************************\

private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
    if (args.Severity == XmlSeverityType.Warning)
        throw new Exception("\tWarning: Matching schema not found.  No validation     occurred." + args.Message);
    else
        throw new Exception("\tValidation error: " + args.Message);    
}

My problem is that I am always getting a validation error with the following message:

Validation error: The element 'Task' has invalid child element 'GoTo'. List of possible elements expected: 'Rotate, SoundRecord, SoundPlayback'.

Do you know of a way I could validate my XML files against my schema that would just check that the correct element types are there but do not care of the order? Or do you know if I could change something to the schema so that the XML files pass validation? Or is the form of my XML a bad practice and there is no way it could pass schema valdation? :)

I would really appreciate any help. Thank you

0

1 Answer 1

1

We can replace the xs:sequence, in which the values are required to be in a particular order, with an unlimited xs:choice which should achieve the desired result.

Try this. Note I have replaced <xs:sequence> with <xs:choice>. Note also the attributes which allow as many selections as you want.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Task">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded"><!-- Here is the change -->
        <xs:element name="GoTo" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="X" type="xs:string" minOccurs="0" />
              <xs:element name="Y" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="MoveForward" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Distance" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Rotate" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Degrees" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
            </xs:sequence>
            <xs:attribute name="Direction" type="xs:string" />
            <xs:attribute name="Time" type="xs:string" />
          </xs:complexType>
        </xs:element>
        <xs:element name="SoundRecord" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Time" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="SoundPlayback" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Time" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
      <xs:attribute name="StartPosition" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="Task" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.