Solved

Reading XML with C#

Posted on 2011-09-03
6
362 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 62

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 74

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

Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this video we show how to create a Shared Mailbox 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 >> Sha…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now