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
Solved

Looping through elements in XML

Posted on 2008-10-22
2
2,102 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HttpPostedFile 1 29
Fulfillment API php code sample 1 58
I dont know why my code is no longer working help please 9 62
VB.Net. Reading xml value 6 29
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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