[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Reading XML with C#

Posted on 2011-09-03
6
Medium Priority
?
381 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 668 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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


Fernando
0
 
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();

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 664 total points
ID: 36502527
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!

Question has a verified solution.

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

How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
I don't pretend to be an expert at this, but I have found a few things that are useful. I hope that sharing them here will help others, so they will not have to face some rather hard choices. Since I felt this to be a topic of enough importance and…
In this video we show how to create a mailbox database 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 >> Data…
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…
Suggested Courses

656 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