c#在未知的嵌套元素上使用XSD验证XML

时间:2017-04-13 07:20:35

标签: c# xml xsd-validation

是否可以创建XSD来验证(未知)嵌套结构? 我最近将我的XML数据更改为分层

<root>
    <record Object_Number="1" Object_Level="1" Object_Heading="Introduction" Object_Text="" Milestones="" Unique_ID="" Field_type="Info" SG_attribute="" Object_Identifier="1" Object_URL="doors://D1DDBAPP04:36677/?version=2&amp;prodID=0&amp;view=0000001a&amp;urn=urn:telelogic::1-432aa0956f684cff-O-1-00028f60">
        <record Object_Number="1.1" Object_Level="2" Object_Heading="What is " Object_Text="" Milestones="" Unique_ID="" Field_type="Info" SG_attribute="" Object_Identifier="23" Object_URL="doors://D1DDBAPP04:36677/?version=2&amp;prodID=0&amp;view=0000001a&amp;urn=urn:telelogic::1-432aa0956f684cff-O-23-00028f60">
            <record Object_Number="1.1.0-1" Object_Level="3" Object_Heading="" Object_Text="This document is, in fact, a database that summarizes specification and different aspects of diagnostic functions implemented in the system." Milestones="" Unique_ID="" Field_type="Info" SG_attribute="" Object_Identifier="24" Object_URL="doors://D1DDBAPP04:36677/?version=2&amp;prodID=0&amp;view=0000001a&amp;urn=urn:telelogic::1-432aa0956f684cff-O-24-00028f60"></record></record>
        <record Object_Number="1.2" Object_Level="2" Object_Heading="Review Checklist" 

这是因为原始数据是分层的,使用XML嵌套功能而不是普通记录更有意义。

所以现在我已经嵌套了

唯一已知的事情是每条记录都有精确的属性,如果其中一条记录具有不正确(不一致)的属性列表和/或不正确的值,我想检查并发现错误。

对于旧的(普通记录,不是嵌套记录),我使用它来验证

xdoc = XDocument.Load(myXml);
                   string xsdMarkup = @"
<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  <xs:element name='root'>
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs='1' maxOccurs='unbounded' name='record'>
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs='1' maxOccurs='1' name='Object_Number' type='xs:string' />
              <xs:element minOccurs='1' maxOccurs='1' name='Object_Level' type='xs:unsignedByte' />
              <xs:element minOccurs='1' maxOccurs='1' name='Object_Heading' type='xs:string' />
              <xs:element minOccurs='1' maxOccurs='1' name='Object_Text' type='xs:string' />
              <xs:element minOccurs='1' maxOccurs='1' name='Milestones' type='xs:string' />
              <xs:element minOccurs='1' maxOccurs='1' name='Unique_ID' type='xs:string' />
              <xs:element minOccurs='1' maxOccurs='1' name='Field_type' type='xs:string' />
              <xs:element minOccurs='1' maxOccurs='1' name='SG_attribute' type='xs:string' />
              <xs:element minOccurs='1' maxOccurs='1' name='Object_Identifier' type='xs:unsignedShort' />
              <xs:element minOccurs='1' maxOccurs='1' name='Object_URL' type='xs:string' />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
";
                    XmlSchemaSet schemas = new XmlSchemaSet();
                    schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));

                    string msg = "";
                    xdoc.Validate(schemas, (o, err) =>
                    {
                        msg += err.Message;
                        //Console.WriteLine("{0}", err.Message);
                        //errors = true;
                    });

                    if (msg == "")
                        e.Result = true;

我使用Visual Studio生成的XSD架构。

现在,对于分层数据,生成的XSD似乎遵循深层结构,这对于已知级别的深度很好,但是对于另一个深度会失败

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="record">
          <xs:complexType>
            <xs:sequence minOccurs="0">
              <xs:element maxOccurs="unbounded" name="record">
                <xs:complexType>
                  <xs:sequence minOccurs="0">
                    <xs:element maxOccurs="unbounded" name="record">
                      <xs:complexType>
                        <xs:sequence minOccurs="0">
                          <xs:element maxOccurs="unbounded" name="record">
                            <xs:complexType>
                              <xs:attribute name="Object_Number" type="xs:string" use="required" />
                              <xs:attribute name="Object_Level" type="xs:unsignedByte" use="required" />
                              <xs:attribute name="Object_Heading" type="xs:string" use="required" />
                              <xs:attribute name="Object_Text" type="xs:string" use="required" />
                              <xs:attribute name="Milestones" type="xs:string" use="required" />
                              <xs:attribute name="Unique_ID" type="xs:string" use="required" />
                              <xs:attribute name="Field_type" type="xs:string" use="required" />
                              <xs:attribute name="SG_attribute" type="xs:string" use="required" />
                              <xs:attribute name="Object_Identifier" type="xs:unsignedShort" use="required" />
                              <xs:attribute name="Object_URL" type="xs:string" use="required" />
                            </xs:complexType>
...
...
...

那么可以用XSD检查嵌套方案吗?

非常感谢提示,

1 个答案:

答案 0 :(得分:0)

以递归方式定义结构

<xs:element name="record">
  <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="record"/>
      ....