none
LINQ: How to get the last XML Child Node? RRS feed

  • Question

  • I have an XML that looks like the <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="8" id="8">xml</g> below:
    How do I get the last node?

    I do not want to pass an ID but rather just get the element that is last for color.
    I am doing something like this but it relies on the ID:

    List<XElement> node = (from el in exampleParent.Descendants("red")
    where el.Element("id").Value == "3"
    select el).ToList();

    <color>
    
    <red id="1">
    </red>
    
    <red id="2">
    </red>
    
    <red id="3">
    </red>
    
    </color>




    • Edited by Open Galaxy Wednesday, March 20, 2019 3:58 PM
    Wednesday, March 20, 2019 2:55 PM

All replies

  • List<XElement> node = (from el in exampleParent.Descendants("red")
    selected).Last();

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.last?view=netframework-4.7.2

    Wednesday, March 20, 2019 3:38 PM
  • List<XElement> node = (from el in exampleParent.Descendants("red")
    selected).Last();

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.last?view=netframework-4.7.2


    Thank you.
    That extension method does not work.
    Maybe there was a typo in my code but  Last is not contained in the definition.
    Only LastNode and that is not working either.

    select el.Last()).ToList()





    • Edited by Open Galaxy Wednesday, March 20, 2019 4:01 PM
    Wednesday, March 20, 2019 4:01 PM
  • List<XElement> node = (from el in exampleParent.Descendants("red")
    select el).ToList();
    
    var last = node.last();
    

    Wednesday, March 20, 2019 5:16 PM
  • Try this too:

       XElement last = exampleParent.Elements( "red" ).LastOrDefault( );


    Or use Descendats instead of Elements in some cases.
    • Edited by Viorel_MVP Wednesday, March 20, 2019 7:09 PM
    Wednesday, March 20, 2019 7:07 PM
  • Try this too:

       XElement last = exampleParent.Elements( "red" ).LastOrDefault( );


    Or use Descendats instead of Elements in some cases.

    So how would this work if I had multiple parents and I wanted to get the last child of each parent? I think this is where the issue is.

    • Edited by Open Galaxy Wednesday, March 20, 2019 7:32 PM
    Wednesday, March 20, 2019 7:15 PM
  • https://github.com/csharpfritz/dotnet-docs/blob/master/docs/csharp/programming-guide/concepts/linq/projections-and-transformations-linq-to-xml.md

    https://github.com/csharpfritz/dotnet-docs/blob/master/docs/csharp/programming-guide/concepts/linq/how-to-project-a-new-type-linq-to-xml.md

    You could project out a new type and do the ToList(). The ToList() will make a collection of objects and then you can use the collection.Last().

    You can also project out an anaymous type as opposed to a concrete type/a class that's in the example.

    Wednesday, March 20, 2019 9:05 PM
  • Hi

    Thank you for posting here.

    For your question, you want to get the last child of each parent by using LINQ.

    You could try the following code.

              string files = @"D:\test.xml";
                XDocument xml = XDocument.Load(files);
                List<XElement> node = (from members in xml.Root.Elements() select (XElement)members.LastNode).ToList();
                foreach (var m in node)
                {
                    Console.WriteLine(m);
                }
                Console.ReadKey();
    

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <Hello>
    <color>
    <red id="1">
    </red>
    <red id="2">
    </red>
    <red id="3">
    </red>
    </color>
    <test>
    <red id="4">
    </red>
    <red id="5">
    </red>
    <red id="6">
    </red>
    </test>
    <example>
    <red id="7">
    </red>
    <red id="8">
    </red>
    <red id="9">
    </red>
    </example>
    </Hello>
    

    Result:

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 21, 2019 3:23 AM
    Moderator