2009-10-20 35 views
5

我不明白爲什麼這個節點列表爲空XmlNodeList中(這是爲什麼空)

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> 

在最後的劇本我想它包含一個字符串數組它的每個屬性。

謝謝 斯特凡

回答

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"

+0

對於XML命名空間常見問題解答!謝謝。 – 2009-10-20 06:18:03

8

你沒有考慮到文檔的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

甚至0 我每次都忽略「xmlns」,所以我編輯上面的XML文件。 – sschnake 2009-10-20 05:30:29

+0

你給我帶來了正確的道路。坦克你 – sschnake 2009-10-20 06:04:16

+0

很高興我能幫上忙。 – 2009-10-20 06:04:34

0

嘗試:

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

+0

沒有..空。 因此,我現在將發佈完整代碼 – sschnake 2009-10-20 05:41:42