ssc*_*ake 5 c# xml selectnodes
我无法理解为什么这个NodeList是空的
XmlDocument document = new XmlDocument();
document.Load(xmlpath);
XmlNodeList nodes = document.SelectNodes("/StructureResponse/rootItem/attributes/Attribute");
Run Code Online (Sandbox Code Playgroud)
这里是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>
Run Code Online (Sandbox Code Playgroud)
在最终脚本中,我想获得一个包含其中每个属性的数组字符串.
谢谢Stefan
您没有考虑xmlns="http://nts-de-osm1-pxc/webservices/"文档上的XML名称空间()!
好的,你甚至有两个独立的命名空间 - 更新了我的样本.
试试这个:
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);
Run Code Online (Sandbox Code Playgroud)
渣
用户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
Run Code Online (Sandbox Code Playgroud)
我建议对 XML 名称空间进行更多研究。尝试浏览Ronald Bourret 的“XML 命名空间常见问题解答”。
| 归档时间: |
|
| 查看次数: |
3470 次 |
| 最近记录: |