[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loop Through XML Child Nodes and Retrieve Name and Value

Posted on 2008-11-06
3
Medium Priority
?
1,590 Views
Last Modified: 2012-06-21
If I have the following XML

<site>
<foo>true</foo>
<bar>true</bar>
</site>

How do i loop through all the child nodes in <site> and then programatically get the name of the child node and the innerXML? (ex. Foo and true)  I'd like to use a nodelist if possible.
0
Comment
Question by:davidcahan
  • 3
3 Comments
 
LVL 2

Expert Comment

by:MortenSlotKristensen
ID: 22899293
Something like this should work:
string xmlstr = @"<site><foo>true</foo><bar>true</bar></site>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstr);
XmlNode root = doc.DocumentElement;
IEnumerator ienum = root.GetEnumerator();
 
while (ienum.MoveNext()) {     
    XmlNode site = (XmlNode) ienum.Current;
    IEnumerator ienum2 = site.GetEnumerator();
 
    while (ienum2.MoveNext()) {
        XmlNode elm = (XmlNode) ienum2.Current;
        Console.WriteLine("Name: "+elm.Name);
        Console.WriteLine("InnerXml: "+elm.InnerXml);
        Console.WriteLine();
    }
 }

Open in new window

0
 
LVL 2

Expert Comment

by:MortenSlotKristensen
ID: 22899373
Now that I think about it.. This is sufficient:
string xmlstr = @"<site><foo>true</foo><bar>true</bar></site>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstr);
XmlNode root = doc.DocumentElement;
IEnumerator ienum = root.GetEnumerator();
while (ienum.MoveNext()) {
   XmlNode elm = (XmlNode) ienum.Current;
   Console.WriteLine("Name: "+elm.Name);
   Console.WriteLine("InnerXml: "+elm.InnerXml);
   Console.WriteLine();
}

Open in new window

0
 
LVL 2

Accepted Solution

by:
MortenSlotKristensen earned 1000 total points
ID: 22899424
And if you would like the NodeList:
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList elements = root.ChildNodes;
 
for(int i = 0; i < elements.Count; i++)
    Console.WriteLine("Name: {0}, InnerXml: {1}", elements[i].Name, elements[i].InnerXml);

Open in new window

0

Featured Post

Industry Leaders: 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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 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