我不明白为什么这个节点列表是空

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>

在最终脚本我想获得其中载有它的每一个属性的阵列字符串。

感谢您 斯蒂芬

有帮助吗?

解决方案

用户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命名空间的研究。尝试略读罗纳德Bourret的 “XML命名空间FAQ”

其他提示

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

OK,你甚至有两个不同的命名空间 - 更新了我的样品

尝试这种情况:

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

马克

尝试:

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top