2013-06-03 14 views
7

Voglio ottenere il valore dal file XML ma non sono riuscito. Potete per favore aiutarmi a sottolineare il problema ?? Perché ho già provato molto duramente a testare e googling ma non riesco ancora a individuare il problema.C# XmlDocument SelectNodes non funziona

XML:

<?xml version="1.0" encoding="utf-8" ?> 
<Contacts> 
    - <Contact> 
    <ID>xxx</ID> 
     <AutoUpdateEnabled>false</AutoUpdateEnabled> 
     <LastChanged>2013-05-29T01:53:59.4470000Z</LastChanged> 
    - <Profiles> 
     - <Personal> 
       <FirstName>My First Name</FirstName> 
       <LastName>My Last Name</LastName> 
       <UniqueName>My Unique Name</UniqueName> 
       <SortName></SortName> 
       <DisplayName>My Display Name</DisplayName> 
      </Personal> 
    </Profiles> 
    - <Phones> 
     - <Phone> 
      <ID>3</ID> 
      <PhoneType>Mobile</PhoneType> 
      <Number>000-0000000</Number> 
      <IsIMEnabled>false</IsIMEnabled> 
      <IsDefault>false</IsDefault> 
      </Phone> 
    </Phones> 
    - <Locations> 
     - <Location> 
       <ID>2</ID> 
       <LocationType>Business</LocationType> 
       <CompanyName></CompanyName> 
       <IsDefault>false</IsDefault> 
      </Location> 
     </Locations> 
</Contact> 
- <Contact> 
    <ID>xxx</ID> 
    <AutoUpdateEnabled>false</AutoUpdateEnabled> 
    <LastChanged>2013-05-29T01:53:25.2670000Z</LastChanged> 
    - <Profiles> 
     - <Personal> 
       <FirstName>Person</FirstName> 
       <LastName>Two</LastName> 
       <UniqueName></UniqueName> 
       <SortName></SortName> 
       <DisplayName>Person Two</DisplayName> 
      </Personal> 
     </Profiles> 
    - <Emails> 
     - <Email> 
       <ID>1</ID> 
       <EmailType>Personal</EmailType> 
       <Address>[email protected]</Address> 
       <IsIMEnabled>false</IsIMEnabled> 
       <IsDefault>true</IsDefault> 
      </Email> 
     </Emails> 
    - <Locations> 
     - <Location> 
       <ID>2</ID> 
       <LocationType>Business</LocationType> 
       <CompanyName>Testing Company</CompanyName> 
       <IsDefault>false</IsDefault> 
      </Location> 
     </Locations> 
    </Contact> 
</Contacts> 

mio codice di esempio:

XmlDocument xmldoc = new XmlDocument(); 
xmldoc.LoadXml("TheXMLFile.xml"); 

xmldoc.DocumentElement.SelectNodes("contact") // return 0 counts 
xmldoc.DocumentElement.SelectNodes("/contact") // return 0 counts 
xmldoc.DocumentElement.SelectNodes("/contact") // return 0 counts 
xmldoc.DocumentElement.SelectNodes("/contacts/contact") // return 0 counts 
xmldoc.DocumentElement.SelectNodes("*") // return 2 counts !this works 

XmlNodeList elemList = xmldoc.DocumentElement.GetElementsByTagName("contact"); // return 2 counts !this also works 
foreach (XmlNode node in elemList) 
{  
    node.SelectSingleNode("Profiles") //return "" 
    node.SelectSingleNode("/Profiles") //return "" 
    node.SelectSingleNode("//Profiles") //return "" 
    node.SelectSingleNode(".//Profiles") //return "" 
} 

voglio solo ottenere "Nome, Cognome, Indirizzo e-mail", la funzione SelectNodes proprio non funziona come previsto ... No indizio a tutti ... per favore aiuto. Grazie in anticipo

risposta

10

avete bisogno di qualcosa di simile:

XmlDocument xmldoc = new XmlDocument(); 
xmldoc.Load(@"D:\temp\contacts.xml"); // use the .Load() method - not .LoadXml() !! 

// get a list of all <Contact> nodes 
XmlNodeList listOfContacts = xmldoc.SelectNodes("/Contacts/Contact"); 

// iterate over the <Contact> nodes 
foreach (XmlNode singleContact in listOfContacts) 
{ 
    // get the Profiles/Personal subnode 
    XmlNode personalNode = singleContact.SelectSingleNode("Profiles/Personal"); 

    // get the values from the <Personal> node 
    if (personalNode != null) 
    { 
     string firstName = personalNode.SelectSingleNode("FirstName").InnerText; 
     string lastName = personalNode.SelectSingleNode("LastName").InnerText; 
    } 

    // get the <Email> nodes 
    XmlNodeList emailNodes = singleContact.SelectNodes("Emails/Email"); 

    foreach (XmlNode emailNode in emailNodes) 
    { 
     string emailTyp = emailNode.SelectSingleNode("EmailType").InnerText; 
     string emailAddress = emailNode.SelectSingleNode("Address").InnerText; 
    } 
} 

Con questo approccio, si dovrebbe essere in grado di leggere tutti i dati necessari in modo corretto.

+0

posso usare il ciclo foreach per "Contatto" e quindi selezionare una singola email all'interno del ciclo ?? – user2402624

+0

@ user2402624: se si desidera una sola e-mail (eventualmente multipla) - sì, è possibile farlo. –

+1

So che questo è un vecchio post, ma da quello che posso vedere LoadXML non funziona se vuoi SelectNodes da un XMLNodeList (con istruzioni Xpath). Ho ottenuto gli stessi risultati - 0 indipendentemente da come ho organizzato Xpath. C'è una soluzione per questo bug? – MC9000

0

I tag XML dipendono dal caso, quindi contattare! = Contatto.

Cambiare questo per un inizio.