none
What Descendants does or return RRS feed

  • Question

  • tell me what XDocument's Descendants function does or return.

    XDocument XDoc = XDocument.Load(xmlFilePath);
                var items = (from item in XDoc.Descendants("TickerBrokerDateFormatMap")
                             select item).ToList();

    please make me understand with example. thanks


    Monday, November 18, 2019 7:56 PM

Answers

  • Hi Sudip_inn, 

    Thank you for posting here.

    Descendants() returns all matched descendant element  in XML.

    I make a simple test on my side.

    My xml.

    Here’s my code.

          var items = (from item in XDoc.Descendants("child1")
                             select item).ToList();
    

    I get three results:

    When I use ‘ele1’ to make a test.

    Result:

        

    Finally, I use ‘parent’ to make a test.

        

    Hope it can help you.

    Besides, you can refer to the following reference about when you should or shouldn’t use Descendants() method.

    Why (or when) you should/shouldn’t use Descendants() method

    Best Regards,

    Xingyu Zhao



    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.

    • Marked as answer by Sudip_inn Tuesday, November 19, 2019 3:32 PM
    Tuesday, November 19, 2019 7:18 AM
    Moderator

All replies

  • Best to read the documentation

    https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xcontainer.descendants?view=netframework-4.8


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, November 18, 2019 8:27 PM
    Moderator
  • Hi Sudip_inn, 

    Thank you for posting here.

    Descendants() returns all matched descendant element  in XML.

    I make a simple test on my side.

    My xml.

    Here’s my code.

          var items = (from item in XDoc.Descendants("child1")
                             select item).ToList();
    

    I get three results:

    When I use ‘ele1’ to make a test.

    Result:

        

    Finally, I use ‘parent’ to make a test.

        

    Hope it can help you.

    Besides, you can refer to the following reference about when you should or shouldn’t use Descendants() method.

    Why (or when) you should/shouldn’t use Descendants() method

    Best Regards,

    Xingyu Zhao



    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.

    • Marked as answer by Sudip_inn Tuesday, November 19, 2019 3:32 PM
    Tuesday, November 19, 2019 7:18 AM
    Moderator