C#中xml和xsd的比较

时间:2014-04-03 08:03:53

标签: xml xsd

我想比较2 xmls的模式结构。出于这个目的,我从一个xml创建了xsd,然后将xsd(模式结构)与另一个xml进行比较...这样我就可以知道两个xml是否具有相同的结构或不

以下是xml

    <?xml version="1.0"?>
    <bookstore xmlns="generic">
       <book genre="autobiography">
           <title>The Autobiography of Benjamin Franklin</title>
           <author>
               <first-name>Ben</first-name>
               <last-name>Franklin</last-name>
           </author>
           <price>89.88</price>
       </book>
       <book genre="novel">
           <title>The Confidence Man</title>
           <author>
               <first-name>John</first-name>
               <last-name>Melville</last-name>
           </author>
           <price>11.99</price>
       </book>
    </bookstore>

and following is generated  xsd from above xml

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="bookstore" targetNamespace="generic" xmlns:mstns="generic" xmlns="generic" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:element name="bookstore" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="book">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
              <xs:element name="price" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
              <xs:element name="author" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="first-name" type="xs:string" minOccurs="0" />
                    <xs:element name="last-name" type="xs:string" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="genre" form="unqualified" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

现在当我将xml与xsd与以下代码进行比较时:

try
            {
                XmlTextReader tr = new XmlTextReader(xsd);
                XmlSchemaSet schema = new XmlSchemaSet();
                schema.Add(null, tr);

                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.IgnoreComments = true;
                settings.IgnoreWhitespace = true;
                settings.IgnoreProcessingInstructions = true;
                settings.Schemas.Add(schema);
                settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
                settings.ValidationEventHandler += new ValidationEventHandler(ErrorHandler);

                XmlReader reader = XmlReader.Create(xml, settings);

                // Validate XML data
                while (reader.Read()) ;
                reader.Close();

                // exception if validation failed
                if (numErrors > 0)
                    throw new Exception(a);

                tr.Close();
                a = "Validation Successful\r\n";
                return a;
            }
            catch
            {
                a = "Validation failed\r\n" + a;
                return a;
            }

验证失败,因为xsd和xml中的顺序是不同的。如果价格不合适........... 我的问题是如何忽略订单..

帮助将会很明显

0 个答案:

没有答案