使用自定义架构/命名空间解析XML

时间:2012-04-17 18:13:04

标签: c# xml xsd xml-parsing

我为XML配置文件编写了一个XSD,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:t="http://www.foo.com/schemas/datatransfer"
           targetNamespace="http://www.foo.com/schemas/datatransfer"
           attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="transferGroups">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="transferGroup" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="connectionString" type="xs:string" />
              <xs:choice minOccurs="1" maxOccurs="1">
                <xs:element name="script" minOccurs="0" maxOccurs="1">
                  <xs:complexType>
                    <xs:attribute name="path" type="xs:string" use="required" />
                    <xs:attribute name="fileName" type="xs:string" use="required" />
                    <xs:attribute name="useCompression" type="xs:boolean" use="required" />
                  </xs:complexType>
                </xs:element>
                <xs:element name="table" minOccurs="0" maxOccurs="1">
                  <xs:complexType>
                    <xs:attribute name="name" type="xs:string" use="required" />
                    <xs:attribute name="fileName" type="xs:string" use="required" />
                    <xs:attribute name="useCompression" type="xs:boolean" use="required" />
                  </xs:complexType>
                </xs:element>
                <xs:element name="tables" minOccurs="0" maxOccurs="1">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="table" maxOccurs="unbounded" minOccurs="1">
                        <xs:complexType>
                          <xs:attribute name="name" type="xs:string" use="required" />
                          <xs:attribute name="fileName" type="xs:string" use="required" />
                          <xs:attribute name="useCompression" type="xs:boolean" use="required" />
                        </xs:complexType>
                      </xs:element>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:choice>
              <xs:element name="format">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="addHeaderRow">
                      <xs:complexType>
                        <xs:attribute name="value" type="xs:boolean" use="required" />
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="columnsDelimitedBy">
                      <xs:complexType>
                        <xs:attribute name="value" type="xs:string" use="required" />
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="rowsDelimitedBy">
                      <xs:complexType>
                        <xs:attribute name="value" type="xs:string" use="required" />
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="transferSite">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="protocol">
                      <xs:complexType>
                        <xs:attribute name="value" type="xs:string" use="required" />
                        <xs:attribute name="address" type="xs:string" use="required" />
                        <xs:attribute name="port" type="xs:unsignedByte" use="required" />
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="credentials">
                      <xs:complexType>
                        <xs:attribute name="userName" type="xs:string" use="required" />
                        <xs:attribute name="password" type="xs:string" use="required" />
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="destinationFolder">
                      <xs:complexType>
                        <xs:attribute name="value" type="xs:string" use="required" />
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="useCompression" type="xs:boolean" use="required" />
            <xs:attribute name="fileName" type="xs:string" use="optional" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我从我在Visual Studio中设计的XML文件生成(然后修改)了模式,如下所示:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<transferGroups>
  <transferGroup useCompression="false" >
    <connectionString>DSN=MyDSN;uid=foo;password=bar;</connectionString>
    <tables>
      <table name="dbo.Foo1" fileName="E:\Files\Foo1_{0:yyyyMMdd}.gz" useCompression="true" />
      <table name="pub.Foo2" fileName="E:\Files\Foo2_{0:yyyyMMdd}.gz" useCompression="true" />
    </tables>
    <format>
      <addHeaderRow value="true"/>
      <columnsDelimitedBy value="|" />
      <rowsDelimitedBy value="\r\n" />
    </format>
    <transferSite>
      <protocol value="FTP" address="localhost" port="21" />
      <credentials userName="anonymous" password="test@myserver.com" />
      <destinationFolder value="/" />
    </transferSite>
  </transferGroup>
</transferGroups>

实际上,我做了一个像这样的配置文件:

<transferGroups xmlns="http://www.foo.com/schemas/datatransfer">
  <transferGroup useCompression="false" >
    <connectionString>some connection string</connectionString>
    <tables>
      <table name="tableName1" fileName="tableName1_{0:yyyyMMdd}.gz" useCompression="true" />
      <table name="tableName2" fileName="tableName2_{0:yyyyMMdd}.gz" useCompression="true" />
    </tables>
    <format>
      <addHeaderRow value="true"/>
      <columnsDelimitedBy value="|" />
      <rowsDelimitedBy value="\r\n" />
    </format>
    <transferSite>
      <protocol value="FTP" address="ftp.someplace.com" port="21" />
      <credentials userName="foo" password="bar" />
      <destinationFolder value="subFolder1" />
    </transferSite>
  </transferGroup>
</transferGroups>
但是,我没有意识到指定名称空间/模式改变了在XmlDocument中解析文件的方式。例如,如果我没有指定架构,这段代码将捕获一个XmlNodeList XmlNode,但是一旦指定了架构,就没有节点匹配:

var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(myXml);
var transferGroupNodes = xmlDoc.SelectNodes("//transferGroups/transferGroup")
    .OfType<XmlNode>();

在这种情况下,如何收集transferGroup节点进行解析?我无法在网上找到一个体面的例子来告诉我如何做到这一点。

1 个答案:

答案 0 :(得分:1)

Schema与您的问题无关 - 您拥有带名称空间的节点,因此需要相应地更改XPath。正如Jon Skeet所指出的,LINQ to XML将使其更易于使用,但您仍然需要选择具有正确名称空间的节点。

在XPath中使用名称空间的问题被多次涵盖 - 从FAQ答案开始 - Using Xpath With Default Namespace in C#

使用XPath忽略名称空间的作弊方式:"//*[ local-name() = 'justName']"