C# XmlNodeList(为什么为空)
我不明白为什么这个节点列表是空的C# XmlNodeList(为什么为空),c#,xml,selectnodes,C#,Xml,Selectnodes,我不明白为什么这个节点列表是空的 XmlDocument document = new XmlDocument(); document.Load(xmlpath); XmlNodeList nodes = document.SelectNodes("/StructureResponse/rootItem/attributes/Attribute"); 这里是XML文件 <?xml version="1.0" encoding="utf-8"?> <StructureR
XmlDocument document = new XmlDocument();
document.Load(xmlpath);
XmlNodeList nodes = document.SelectNodes("/StructureResponse/rootItem/attributes/Attribute");
这里是XML文件
<?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>
一串
识别号
真实身份
真的
真的
9662744
一串
人工智能
特鲁埃
真的
真的
00
在最后一个脚本中,我想得到一个数组字符串,它包含其中的每个属性
多谢各位
Stefan您没有考虑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);
马克试试:
XmlNodeList nodest nodes=document.SelectNodes(“./structuresponse/rootItem/attributes”)代码>用户marc_的答案实际上是正确的。您需要注意XML名称空间。但是,他的代码示例无法直接用于您的示例。下面是一个完整的示例,可以使用您提供的XML(尽管我不得不清理它…它缺少属性的结束标记)
string xmlData=
@"
.现在甚至是0,我每次都忽略了“xmlns”,所以我编辑上面的XML文件。否..为空。所以我现在发布完整的代码
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