2009-10-20 1 views
5

このNodeListのが最終的なスクリプトでいる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> 

空である理由を私は理解できない私が含まれている配列の文字列を取得したいですその中のすべての属性。

は、ドキュメント上の考慮にXML名前空間(xmlns="http://nts-de-osm1-pxc/webservices/")を取っていないあなたに ステファン

答えて

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

+1 XML名前空間の良いリンク+1ありがとう。 –

8

をありがとうございました!

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

マルク・

+0

これは0です。 私は毎回「xmlns」を無視していましたので、上記のXMLファイルを編集します。 – sschnake

+0

あなたは正しい方法で私を連れて来ました。あなたはタンク – sschnake

+0

助けてくれてうれしいです。 –

0

試してみてください。

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

+0

いいえ..空です。 今すぐフルコードを投稿します – sschnake

関連する問題