【问题标题】:XmlNodeList (why is this empty)XmlNodeList(为什么这是空的)
【发布时间】:2010-12-08 05:38:49
【问题描述】:

我不明白为什么这个 NodeList 是空的

XmlDocument document = new XmlDocument();
document.Load(xmlpath);    
XmlNodeList nodes = document.SelectNodes("/StructureResponse/rootItem/attributes/Attribute");

这里是 XmlFile

<?xml version="1.0" encoding="utf-8"?>
<StructureResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://nts-de-osm1-pxc/webservices/">
    <consolidatedItems xsi:nil="true" xmlns="http://systinet.com/wsdl/com/osm/webservices/service/" />
    <rootItem xsi:type="Part" xmlns="http://systinet.com/wsdl/com/osm/webservices/service/">
        <attributes>
            <Attribute>
                <dataDictionary xsi:nil="true" />
                <dataType>string</dataType>
                <displayName>IDENT_NR</displayName>
                <key>true</key><name>IDENT_NR</name>
                <searchable>true</searchable>
                <userAttribute>true</userAttribute>
                <value>9662744</value>
            </Attribute>
            <Attribute>
                <dataDictionary xsi:nil="true" />
                <dataType>string</dataType>
                <displayName>AI</displayName>
                <key>true</key><name>AI</name>
                <searchable>true</searchable>
                <userAttribute>true</userAttribute>
                <value>00</value>
            </Attribute>
        </rootItem>
    </StructureResponse>

在最终脚本中,我想得到一个数组字符串,其中包含其中的每个属性。

谢谢 斯蒂芬

【问题讨论】:

    标签: c# xml selectnodes


    【解决方案1】:

    您没有考虑文档上的 XML 命名空间 (xmlns="http://nts-de-osm1-pxc/webservices/")!

    好的,你甚至有两个单独的命名空间 - 更新了我的示例。

    试试这个:

    XmlDocument document = new XmlDocument();
    document.Load(xmlpath);    
    
    XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable);
    mgr.AddNamespace("ns", "http://nts-de-osm1-pxc/webservices/"); 
    mgr.AddNamespace("root", "http://systinet.com/wsdl/com/osm/webservices/service/");
    
    XmlNodeList nodes = document.SelectNodes("/ns:StructureResponse/root:rootItem/root:attributes/root:Attribute", mgr);
    

    马克

    【讨论】:

    • 甚至是0 我每次都忽略了“xmlns”,所以我编辑了上面的XML文件。
    【解决方案2】:

    试试:

    XmlNodeList nodes = document.SelectNodes("./StructureResponse/rootItem/attributes");

    【讨论】:

    • 没有 .. 空的。所以我现在将发布完整代码
    【解决方案3】:

    用户 marc_s 的回答实际上是正确的。您需要注意 XML 名称空间。但是,他的代码示例不能直接用于您的示例。这是与您提供的 XML 一起使用的完整示例(尽管我必须清理它...它缺少 attributes 的结束标记)。

    string xmlData = 
    @"<?xml version='1.0' encoding='utf-8'?>
      <StructureResponse
         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
         xmlns:xsd='http://www.w3.org/2001/XMLSchema'
         xmlns='http://nts-de-osm1-pxc/webservices/'>
        <consolidatedItems xsi:nil='true' xmlns='http://systinet.com/wsdl/com/osm/webservices/service/' />
        <rootItem xsi:type='Part' xmlns='http://systinet.com/wsdl/com/osm/webservices/service/'>
          <attributes>
            <Attribute>
              <dataDictionary xsi:nil='true' />
              <dataType>string</dataType>
              <displayName>IDENT_NR</displayName>
              <key>true</key>
              <name>IDENT_NR</name>
              <searchable>true</searchable>
              <userAttribute>true</userAttribute>
              <value>9662744</value>
            </Attribute>
            <Attribute>
              <dataDictionary xsi:nil='true' />
              <dataType>string</dataType>
              <displayName>AI</displayName>
              <key>true</key>
              <name>AI</name>
              <searchable>true</searchable>
              <userAttribute>true</userAttribute>
              <value>00</value>
            </Attribute>
          </attributes>
          </rootItem>
      </StructureResponse>";
    
    XmlDocument document = new XmlDocument();
    XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
    namespaceManager.AddNamespace("a", "http://nts-de-osm1-pxc/webservices/");
    namespaceManager.AddNamespace("b", "http://systinet.com/wsdl/com/osm/webservices/service/");
    document.LoadXml(xmlData);
    XmlNodeList nodes = document.SelectNodes("/a:StructureResponse/b:rootItem/b:attributes/b:Attribute", namespaceManager);
    // 'nodes' contains 2 items now, as expected
    

    我建议对 XML 命名空间进行更多研究。尝试略读Ronald Bourret's "XML Namespaces FAQ"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 2021-12-12
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      相关资源
      最近更新 更多