Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Looping through elements in XML

Posted on 2008-10-22
2
Medium Priority
?
2,109 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 500 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 500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

972 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