• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

Reading XML with C#

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
jmkotman
Asked:
jmkotman
3 Solutions
 
pivarCommented:
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
 
jmkotmanAuthor Commented:
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
 
pivarCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Fernando SotoCommented:
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
 
käµfm³d 👽Commented:
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

Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now