Reading XML with C#

Posted on 2011-09-03
Medium Priority
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.

<FullName>Adam&apos;s Candy Shop</FullName>
<FullName>Andres, Cristina</FullName>
Question by:jmkotman
LVL 22

Expert Comment

ID: 36478602


 XmlElement root = doc.DocumentElement;
 XmlNodeList nodeList =
      foreach (XmlNode node in nodeList){


Author Comment

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.
LVL 22

Accepted Solution

pivar earned 668 total points
ID: 36478642

 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){
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

LVL 64

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

LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 668 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();


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

Open in new window

LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 664 total points
ID: 36502527

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

What is the biggest problem in managing an exchange environment today? It is the lack of backups, disaster recovery (DR) plan, testing of the DR plan or believing that it won’t happen to us.
There’s hardly a doubt that Business Communication is indispensable for both enterprises and small businesses, and if there is an email system outage owing to Exchange server failure, it definitely results in loss of productivity.
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
This video discusses moving either the default database or any database to a new volume.

600 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