2009-10-20 15 views
5

non riesco a capire il motivo per cui questo NodeList è VuotoXmlNodeList (perché è questo vuoto)

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

Qui il 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> 

Nel copione finale voglio ottenere una stringa che contiene serie ogni attributo in esso.

Grazie Stefan

risposta

3

utente è in realtà corretto. È necessario prestare attenzione agli spazi dei nomi XML. Il suo esempio di codice, tuttavia, non funzionerà direttamente per il tuo esempio. Ecco un esempio completo che funziona con l'XML che hai fornito (anche se ho dovuto ripulirlo ... mancava un tag di chiusura per 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 

Suggerisco di approfondire lo studio dei domini XML. Prova a scremare Ronald Bourret's "XML Namespaces FAQ".

+0

+1 buon link per le domande frequenti sugli spazi dei nomi XML! Grazie. –

8

Non stai tenendo conto del namespace XML (xmlns="http://nts-de-osm1-pxc/webservices/") sul documento!

OK, hai persino due spazi dei nomi separati: ho aggiornato il mio esempio.

Prova questa:

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

È pari a 0 Ho ignorato gli "xmlns" ogni volta, quindi ho modificato il file XML sopra. – sschnake

+0

Mi hai portato sulla buona strada. tank you – sschnake

+0

Contento di poterti aiutare. –

0

Prova: la risposta di marc_s

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

+0

No .. Vuoto. Quindi posterò il codice completo ora – sschnake