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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2674
  • Last Modified:

Looping through XML using LINQ / XMLDocument

Hi - Could someone please help me.

I am looking for a simple way to loop through an XML string and all its elements using LINQ, and ouputting each Node / Element / Attribute as it loops through the data.

The XML is listed below. It needs to be dynamic though - no reference to any entity names.
I presume it would be some sort of recursive example.

I basically want to use Console.WriteLine to reproduce each element on screen from the xmlString being read. I do NOT want to use an XmlReader or XmlTextReader for this. Using XMLDocument would also be helpful, although I'd prefer LINQ as from what I've read, it's faster.

Thanks
<ediMessage>
<UNH>
    <unhCode testCode='TST0001'>100</unhCode>
    <unhMessageType>CUSDEC</unhMessageType>
    <unhShortCode>CDSA</unhShortCode>
    <unhInfo>
        <unhInfoName>Paul</unhInfoName>
        <unhInfoSurName>Bunyon</unhInfoSurName>
    </unhInfo>
</UNH>
<static>HELLO</static>
<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>
<UNZ>
    <unzCode>ANT01</unzCode>
    <unzId>1234-908-34544-0001</unzId>
</UNZ>
<UNT>
    <untTotalCode>SAL</untTotalCode>
    <untTotal>100 000</untTotal>
</UNT>
</ediMessage>

Open in new window

0
djcheeky
Asked:
djcheeky
  • 4
  • 3
1 Solution
 
Fernando SotoCommented:
Hi djcheeky;

The code snippet below will iterate through all the nodes and write to the console window the following:

Element Name: UNH

Element Name: unhCode    Value: 100
        Attribute Name = testCode    Value = TST0001

Element Name: unhMessageType    Value: CUSDEC

Element Name: unhShortCode    Value: CDSA

Element Name: unhInfo

Element Name: unhInfoName    Value: Paul

Element Name: unhInfoSurName    Value: Bunyon

Element Name: static    Value: HELLO

Element Name: UCM

Element Name: ucmNumber    Value: 12345

Element Name: ucmOrganisation    Value: GreenPeace

Element Name: ucmIndex    Value: 1

Element Name: ucmIndexCode    Value: GP

Element Name: UNT

Element Name: untTotalCode    Value: TAX

Element Name: untTotal    Value: 135 000 000

Element Name: UNT

Element Name: untTotalCode    Value: REBATE

Element Name: untTotal    Value: 35 000 000

Element Name: UNZ

Element Name: unzCode    Value: ANT01

Element Name: unzId    Value: 1234-908-34544-0001

Element Name: UNT

Element Name: untTotalCode    Value: SAL

Element Name: untTotal    Value: 100 000


Fernando

private void button3_Click(object sender, EventArgs e)
{
    XDocument doc = XDocument.Load("Test.xml");
    XElement root = doc.Root;
 
    foreach (XElement element in root.Descendants())
    {
        if (element.HasElements)
        {
            Console.WriteLine("\nElement Name: {0}", element.Name);
        }
        else
        {
            Console.WriteLine("\nElement Name: {0}\tValue: {1}", element.Name, element.Value);
        }
        if (element.HasAttributes)
        {
            foreach (XAttribute attrib in element.Attributes())
            {
                Console.WriteLine("\t\tAttribute Name = {0}\tValue = {1}", attrib.Name, attrib.Value);
            }
        }                
    }
}

Open in new window

0
 
djcheekyAuthor Commented:
Hi Fernando and thanks for the great anwer.

Just one thing though - will this work for multiple nested elements, or just one layer. I need something that can work no matter how many levels of nodes / elements there are!

Thanks!
Paolo
0
 
Fernando SotoCommented:
Hi djcheeky;

It will returns a collection of the descendant elements for this document or element, in  document order. So every element is returned.

Fernando
0
Independent Software Vendors: 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!

 
djcheekyAuthor Commented:
Aah cool  thanks!

Just one more quick question before I go - with XmlReader you hve the Conformance.Fragment Setting that can be applied to the XML - is there something similar for xmlDocument??

Thanks a mill!
0
 
Fernando SotoCommented:
Hi djcheeky;

I have not worked much with XML model using the XML classes. I have started using XML when Linq made it simpler to use, create and edit XML documents. So if the question is does Linq to XML allow you to load and work with XML Fragment the answer is Yes. If that is not the question can you post a link to Conformance.Fragment Setting or explain what it is so that I may answer your question correctly.

Fernando
0
 
djcheekyAuthor Commented:
Thanks Fernando - will do!
0
 
Fernando SotoCommented:
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now