Solved

Looping through elements in XML

Posted on 2008-10-22
2
2,097 Views
Last Modified: 2010-02-12
Hi - I am trying to loop through an xml string as shown in listing A.

When I try run the code in Listing B, I only get the details of the FIRST node (UNH) in the XML displayed, not the rest, like UCM, UNT etc.

Why is this and how do I get the thing to display all nodes??
I suspect its because the nodes have different names?
But surely there must be a away to get all nodes with DIFFERENT names?

Please help
Thanks


//Listing A
 

incomingMessage = @"

<EdiMessage>

<UNH>

    <unhCode>100</unhCode>

    <unhMessageType>CUSDEC</unhMessageType>

    <unhShortCode>CDSA</unhShortCode>

</UNH>

<UCM>

    <ucmNumber>12345</ucmNumber>

    <ucmOrganisation>GreenPeace</ucmOrganisation>

    <ucmIndex>1</ucmIndex>

    <ucmIndexCode>GP</ucmIndexCode>

</UCM>

<UNT>

    <untTotalCode>TAX</untTotalCode>

    <untTotal>135 000 000</untTotal>

</UNT>

<UNT>

    <untTotalCode>REBATE</untTotalCode>

    <untTotal>35 000 000</untTotal>

</UNT>

<UNT>

    <untTotalCode>DIFF</untTotalCode>

    <untTotal>100 000 000</untTotal>

</UNT>

<UNZ>

    <unzCode>ANT01</unzCode>

    <unzId>1234-908-34544-0001</unzId>

</UNZ>

<UNT>

    <untTotalCode>SAL</untTotalCode>

    <untTotal>100 000</untTotal>

</UNT>

<UNT>

    <untTotalCode>COST</untTotalCode>

    <untTotal>20 000</untTotal>

</UNT>

</EdiMessage>";
 

//Listing B
 

 xmlIn.LoadXml(incomingMessage);

           

            XmlNodeList xmlnode = xmlIn.SelectNodes("EdiMessage");

            for (int i = 0; i < xmlnode.Count; i++)

            {

                XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;

                Console.Write(xmlnode[i].FirstChild.Name);

                Console.WriteLine(":\t" + xmlnode[i].FirstChild.InnerText);

            }

Open in new window

0
Comment
Question by:djcheeky
2 Comments
 
LVL 12

Accepted Solution

by:
ShazbotOK earned 125 total points
ID: 22780694
Since "EditMessage" is your ROOT node, use this instead:
XmlNodeList xmlnode = xmlIn.FirstChild.ChildNodes;
0
 
LVL 3

Assisted Solution

by:torrie01
torrie01 earned 125 total points
ID: 22780768
There is only 1 EdiMessage, which is why you only get one node.  Once you get that node, you can just loop through its children, like so:


XmlNode node = xmlIn.SelectSingleNode("EdiMessage");

for (int i = 0; i < node.ChildNodes.Count; i++)

{

     XmlAttributeCollection xmlattrc = node.ChildNodes[i].Attributes;

     Console.Write(node.ChildNodes[i].FirstChild.Name);

     Console.WriteLine(":\t" + node.ChildNodes[i].FirstChild.InnerText);

}

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
(code works) but print excel in Landscape 5 49
ConsoleSql 1 18
Convert to Hour/minute textbox and compare 3 35
XML & .net 5 19
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

23 Experts available now in Live!

Get 1:1 Help Now