使用针对Xsd的混杂子元素验证Xml

时间:2013-05-11 07:42:06

标签: c# xsd xml-validation xsd-validation

我有一个类似下面的XML文件。我正在使用它向机器人发出一系列命令:

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

正如您所看到的,Task元素具有相同类型的子元素,这些子元素不是一个接一个地放置,比如GoTo元素。 我使用Microsoft Visual Studio命令提示符(2010)中的xsd.exe基于上面的XML文件生成此模式:

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

我正在使用以下代码验证我的XML文件,包括此示例中的XML文件:

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

我的问题是我总是收到验证错误,并显示以下消息:

验证错误:元素'Task'具有无效的子元素'GoTo'。预期可能元素的列表:'Rotate,SoundRecord,SoundPlayback'。

您是否知道我可以根据我的架构验证XML文件的方法,只检查是否存在正确的元素类型但不关心订单? 或者你知道我是否可以更改模式的某些内容,以便XML文件通过验证? 或者我的XML形式是一种不好的做法,它无法通过架构的价值? :)

我真的很感激任何帮助。谢谢

1 个答案:

答案 0 :(得分:1)

我们可以使用无限xs:选项替换xs:sequence,其中值必须按特定顺序排列,这应该可以达到预期的结果。

试试这个。注意我已将<xs:sequence>替换为<xs:choice>。另请注意允许任意数量选择的属性。

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