2009-10-20 8 views
5

Ich kann nicht verstehen, warum dieses NodeList Leere istXmlNodeList (warum ist das leer)

XmlDocument document = new XmlDocument(); 
document.Load(xmlpath);  
XmlNodeList nodes = document.SelectNodes("/StructureResponse/rootItem/attributes/Attribute"); 

Hier 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> 

Im Finale Script Ich möchte ein Array String erhalten, die enthält jedes Attribut darin.

Danke Stefan

Antwort

3

Benutzer marc_s Antwort ist eigentlich richtig. Sie müssen auf die XML-Namespaces achten. Sein Codebeispiel funktioniert jedoch nicht direkt für Ihr Beispiel. Hier ist ein vollständiges Beispiel, das mit der XML funktioniert, die Sie angegeben haben (obwohl ich es aufräumen musste ... es fehlte ein schließendes Tag für 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 

Ich schlage vor, ein bisschen mehr lernen von XML-Namespaces. Versuchen Sie, Skimming Ronald Bourret's "XML Namespaces FAQ".

+0

+1 guten Link für XML Namespaces FAQ! Vielen Dank. –

8

Sie sind nicht unter Berücksichtigung des XML-Namespace (xmlns="http://nts-de-osm1-pxc/webservices/") auf dem Dokument!

OK, Sie haben sogar zwei separate Namespaces - aktualisiert mein Beispiel.

Versuchen Sie folgendes:

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

Marc

+0

Es ist sogar 0 Ich ignorierte die "xmlns" jedes Mal, also bearbeite ich die XML-Datei oben. – sschnake

+0

Du hast mich auf den richtigen Weg gebracht. Tank Sie – sschnake

+0

Ich bin froh, dass ich helfen konnte. –

0

Versuchen:

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

+0

Nein. Leer. Also werde ich den vollen Code jetzt – sschnake