linq to xml query returning a list of all child elements RRS feed

  • Question

  • I have got an xml document which looks something like this.

    <Info> .... </Info>
    <Info> .... </Info>
    <response> .... </response>
    <warning> .... </warning>
    <Info> .... </Info>

    How can i write a linqToXML query so that it returns me an IEnumerable containing each child element, in this case all five child elements of , so that i could iterate over them.

    The order of child elements is not definite, neither is number of times the may appear.

    Thanks in advance

    Monday, March 8, 2010 2:51 PM


  • This should do it:
            ' Be sure to set a reference to System.Core and System.Xml.Linq
            Dim root As XElement = XElement.Load("testXML.xml")
            Dim childElements = root.Descendants

    Hope this helps.

    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by DiegoCattaruzza Monday, March 8, 2010 6:10 PM
    • Marked as answer by Xience Thursday, March 11, 2010 9:48 AM
    Monday, March 8, 2010 4:05 PM