locked
Trouble with XElement.Elements() -- No elements are ever returned? RRS feed

  • Question

  •  It is with some hesitation that I post this question as I definitely have the feeling I'm just missing something... however...

    I am having some trouble understanding the behavior of the XElement.Elements() method (System.Xml.Linq).

    Consider this simple XML document...

    <?xml version="1.0" encoding="UTF-8"?>
    <command_set issued_at="1/1/2009" >
      <command name="pan_to">one</command>
      <command name="pan_to">two</command>
    </command_set>


    As I see it, it has a root node, with the name "command_set"; and that root node has two elements within it (both named "command").

    I parse this into an XDocument, then I get a reference to the Root property of that XDocument.  I name the reference to the root element simply "root", like so.

    XDocument doc = XDocument.Parse(that_text_above);

    XElement root = doc.Root;

    Now, if I call root.Element("command"), I get the first "command" element, and that makes sense.  Below I've included a snipet from my Immediate Window in VS.

    root.Element("command")


    <command name="pan_to">one</command>
        base {System.Xml.Linq.XContainer}: <command name="pan_to">one</command>
        FirstAttribute: {name="pan_to"}
        HasAttributes: true
        HasElements: false
        IsEmpty: false
        LastAttribute: {name="pan_to"}
        Name: {command}
        NodeType: Element
        Value: "one"


    However, if I call root.Elements("command"), I get this...

    root.Elements("command")

    {System.Xml.Linq.XContainer.GetElements}
        name: null


    And if I simply call root.Elements(), I get the same thing...

    root.Elements()

    {System.Xml.Linq.XContainer.GetElements}
        name: null


    Also, if I query the HasElements property of root, I think there is clear indication that child elements are present.

    root.HasElements

    true


    I've been back and forth through documentation, but I am now thinking that there is just some fundamental concept that I am missing.  Why do root.Elements()  and root.Elements("command") return nothing in this case?  If anybody could tell me what I'm getting wrong, I would appreciate it massively.

    Wednesday, July 22, 2009 1:08 PM

Answers

  • Hi Pat Daburu,

    root.Elements("command")

    {System.Xml.Linq.XContainer.GetElements}
        name: null

    Elements:   Overloaded. Returns a collection of the child elements of this element or document, in document order

    It is normal.  Because a collection is returned and we didn't set its name property. Thus, it will be null.  However,  we can interate its child like this.

    from el in root.Elements("command")
     select el;

    Best regards,

    Jonathan

    Wednesday, July 29, 2009 7:06 AM

All replies

  • Try root.Descendants()?

     

    Wednesday, July 22, 2009 3:03 PM
  • Try root.Descendants()?



    Thank you for that.  root.Descendants() does the trick in this particular document.  But I am wondering what root.Elements() is supposed to be doing here.  My understanding from what I have read is that the Descendants() method will do a recursive search of all the elements below the current element, whereas Elements() should only consider the immediate child nodes.  So, in this case, I would expect the same results from both methods.

    Do I have that wrong?
    Wednesday, July 22, 2009 3:30 PM
  • It's a good question regarding .Elements(), but to be honest, I've not used the method before.  I typically use .Descendants() and .DescendantsAndSelf() for doing Linq queries against XML data.

    Wednesday, July 22, 2009 3:38 PM
  • Hi Pat Daburu,

    root.Elements("command")

    {System.Xml.Linq.XContainer.GetElements}
        name: null

    Elements:   Overloaded. Returns a collection of the child elements of this element or document, in document order

    It is normal.  Because a collection is returned and we didn't set its name property. Thus, it will be null.  However,  we can interate its child like this.

    from el in root.Elements("command")
     select el;

    Best regards,

    Jonathan

    Wednesday, July 29, 2009 7:06 AM
  •  Thank you, Jonathan.  I was working from a code sample in a Silverlight book that used the Elements() method without the Linq query (?).  The approach you outlined seems to do the trick.  I very much appreciate your attention.

    Thursday, July 30, 2009 11:02 AM
  • [Edit] Deleted my question, ended up being a mistake on my side
    Wednesday, March 3, 2010 2:20 PM
  • Hi All,

    I have xml file below one from this i want to get all parents and child elements and bind to silverlight Treeview how can i do this .

    <Regions>

    <Region Name="qqq"></Regions>

    <subRegion1 Name="qqq"></subRegions1>

    <subRegion2 Name="qqq"></subRegions3>

    </Regions>

    Please anybody can help me on this.

     

     

    Advanced thank you.

    Regards,

    Narasimha

    Tuesday, February 21, 2012 1:32 AM