Solved

Reading XML with C#

Posted on 2011-09-03
6
369 Views
Last Modified: 2013-12-17
I am trying to read this XML file.  In particular I need to access the CustomerTypeRef/FullName value.  I am able to read the CustomerRef/FullName elements fine but not sure how to even begin to read the child elements.
Thanks,
Jon

<QBXML>
<QBXMLMsgsRs>
<CustomerQueryRs>
<CustomerRet>
<FullName>Adam&apos;s Candy Shop</FullName>
<LastName>Bradley</LastName>
<CustomerTypeRef>
<ListID>10000-934380929</ListID>
<FullName>Commercial</FullName>
</CustomerTypeRef>
</CustomerRet>
<CustomerRet>
<FullName>Andres, Cristina</FullName>
<LastName>Andres</LastName>
<CustomerTypeRef>
<ListID>20000-934380929</ListID>
<FullName>Residential</FullName>
</CustomerTypeRef>
</CustomerRet>
0
Comment
Question by:jmkotman
6 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 36478602
Hi,

Try

 XmlElement root = doc.DocumentElement;
 XmlNodeList nodeList =
 root.SelectNodes("/QBXML/QBXMLMsgsRs/CustomerQueryRs/CustomerRet/CustomerTypeRef/FullName");
      foreach (XmlNode node in nodeList){
        Console.WriteLine(node.Value);
      }

/peter
0
 

Author Comment

by:jmkotman
ID: 36478610
How Do I read both at the parent and child at the same time?  Basically, there is only ever one CusterTypRef element so I want to read it at the same time as the CustomerRet element.
0
 
LVL 22

Accepted Solution

by:
pivar earned 167 total points
ID: 36478642
Try


 XmlElement root = doc.DocumentElement;
 XmlNodeList nodeList =  root.SelectNodes("/QBXML/QBXMLMsgsRs/CustomerQueryRs/CustomerRet");
      foreach (XmlNode node in nodeList){
        XmlNodeList nodeList2 = node.SelectNodes("/CustomerTypeRef/FullName");
        foreach (XmlNode node2 in nodeList2){
          Console.WriteLine(node2.InnerText);
        }
      }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 36479405
Hi jmkotman;

If you are using .Net version 3.5 or higher then you can use Linq to XML. The code snippet below should do what you need.

using System.Xml.Linq;

XElement xdoc = XElement.Load("C:/Working Directory/QBXML.xml");

var queryCustomers = from c in xdoc.Descendants("CustomerRet")
                     select new {
                         FullName = (string) c.Element("FullName"),
                         LastName = (string) c.Element("LastName"),
                         ListID = (string) c.Element("CustomerTypeRef").Element("ListID"),
                         FullNameRef = (string) c.Element("CustomerTypeRef").Element("FullName")
                     };
                     
foreach (var customer in queryCustomers)
{
    Console.WriteLine("{0} : {1} : {2} : {3}", customer.FullName, customer.LastName,
        customer.ListID, customer.FullNameRef);
}

Open in new window


Fernando
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 167 total points
ID: 36482283
If you don't have 3.5 available, or you just like the 2.0 way of doing things, then you could use XmlDocument with the appropriate XPath:

using System.Xml;

...

XmlDocument xdoc = new XmlDocument();

xdoc.Load("input.xml")

foreach (XmlNode custRet in xdoc.SelectNodes("//CustomerRet"))
{
    // custRet now represents a CustomerRet node. this is your parent node.

    // to select the CustomerTypeRef/FullName
    XmlNode fullName = custRet.SelectSingleNode("CustomerTypeRef/FullName");

    // Use the value of the node
    Console.WriteLine(fullName.InnerText);
}

Open in new window

0
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 166 total points
ID: 36502527
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
how to add IIS SMTP to handle application/Scanner relays into office 365.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question