locked
Read xml subroot element with Xdocument RRS feed

  • Question

  • Hello

            I have following XML . In which one root and nth(can be 1 , 2 3,  ..... nt) subroot . I want to read the name of all the subroot . BUT I WANT TO READ USING XDOCUMENT

    forexample - output should be 

    subroot1
    subroot2
    subroot3 
    subroot4  

     

    XML as follows

     

    <root>

         <subroot1></subroot1>
        <subroot2></subroot2>
        <subroot3></subroot3>
        <subroot4></subroot4>           to nth....

     

    </root>


    It's Me
    Thursday, May 19, 2011 7:04 AM

Answers

  • Loop over the XDocument.Root.Elements() and you can get the Name of each XElement:

    foreach(XElement e in XDocument.Root.Elements())
    {
        Console.WriteLine(e.Name);
    }
    
    • Marked as answer by It_s Meee Friday, May 20, 2011 7:02 AM
    Thursday, May 19, 2011 12:19 PM

All replies

  • XDocument.Root.Elements(), XElement has a Name property.

     

    Thursday, May 19, 2011 10:08 AM
  • hello

             Many thanks for reply. but i don't know in advance what is the name and how many subroot are there under root node. 


    It's Me
    Thursday, May 19, 2011 12:04 PM
  • Loop over the XDocument.Root.Elements() and you can get the Name of each XElement:

    foreach(XElement e in XDocument.Root.Elements())
    {
        Console.WriteLine(e.Name);
    }
    
    • Marked as answer by It_s Meee Friday, May 20, 2011 7:02 AM
    Thursday, May 19, 2011 12:19 PM