2013-06-03 10 views
7

Ich möchte den Wert aus XML-Datei erhalten, aber ich scheiterte. Kannst du mir bitte helfen, auf das Problem hinzuweisen? Weil ich mich schon sehr bemüht habe zu testen und zu googlen, aber ich kann das Problem immer noch nicht erkennen.C# XmlDocument SelectNodes funktioniert nicht

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> 

Mein Beispielcode:

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 "" 
} 

Ich möchte nur bekommen "Vorname, Nachname, E-Mail-Adresse", die SelectNodes Funktion einfach nicht wie erwartet funktioniert ... Nein Hinweis überhaupt ... bitte helfen. Vielen Dank im Voraus

Antwort

10

Sie brauchen etwas wie folgt aus:

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

Mit diesem Ansatz sollten Sie in der Lage sein, alle Daten, die Sie richtig lesen müssen.

+0

kann ich foreach Schleife für "Kontakt" verwenden und dann eine einzelne E-Mail innerhalb der Schleife auswählen? – user2402624

+0

@ user2402624: Wenn Sie nur eine einzige E-Mail (von möglicherweise mehreren) wollen - ja, können Sie das tun. –

+1

Ich weiß, das ist ein alter Beitrag, aber von dem, was ich sehen kann, funktioniert LoadXML nicht, wenn Sie SelectNodes aus einer XMLNodeList (mit Xpath-Anweisungen) wollen. Ich habe die gleichen Ergebnisse - egal wie ich den Xpath eingerichtet habe. Gibt es eine Lösung für diesen Fehler? – MC9000

0

Die XML-Tags sind fallabhängig, also Kontakt! = Kontakt.

Ändern Sie dies für einen Start.