Solved

Reading XML with C#

Posted on 2011-09-03
6
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Read this checklist to learn more about the 15 things you should never include in an email signature.
In this video we show how to create a Contact 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 Recipients >> Contact ta…
In this video we show how to create an Accepted Domain 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 Mail Flow >> Ac…

724 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