Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3959238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:42:20+00:00 2026-05-20T02:42:20+00:00

Visual Studio 2010, Silverlight 4, and C#. I have the following data stored in

  • 0

Visual Studio 2010, Silverlight 4, and C#. I have the following data stored in an XML file:

<root>
      <element>TextHere</element>
      <element>TextHere</element>
      <element>TextHere</element>
</root>

This is my current code.

XDocument xmlDoc = XDocument.Load("XMLDocument.xml");
var ElementsList = from Elements in xmlDoc.Descendants("root")
                   select new
                   {
                       ElementContent = Elements.Element("Element").Value,
                   };

This code only puts the very first element in the list, leaving all of the others out. How can I rewrite this code so that it will capture ALL of the elements that are named “element” in the XML file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:42:21+00:00Added an answer on May 20, 2026 at 2:42 am

    This would do it:

    XDocument xmlDoc = XDocument.Load("XMLDocument.xml");
    var ElementsList = from Elements in xmlDoc.Descendants("element")
                       select new
                       {
                           ElementContent = Elements.Value
                       };
    

    Or a little more succinct in dot notation:

    var ElementsList = xmlDoc.Descendants("element")
                             .Select(x => new { ElementContent = x.Value });
    

    Note however that you only have an enumeration of elements after this, if you want a list (as your variable name suggests) you can add a .ToList() after the Select:

    var ElementsList = xmlDoc.Descendants("element")
                             .Select(x => new { ElementContent = x.Value })
                             .ToList();
    

    This will list will contain 3 elements (based on your example XML.
    ) of an anonymous type that has a ElementContent property. If you do not need that property (and I would think you don’t) this is a simplified version that just returns a list of string:

    var ElementsList = xmlDoc.Descendants("element")
                             .Select(x => x.Value)
                             .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Silverlight 4, in Visual Studio 2010. I have some Domain Services
I'm using Visual Studio 2010, .Net4, Silverlight(4.0.50826.0). I created a WCF service and I`m
With the recent announcement of .NET 4.0 and Visual Studio 2010, it is becoming
With the recent news about F# being included with Visual Studio 2010, I got
I know it's still in CTP, but is Visual Studio 2010 stable enough for
Visual studio is pretty good but doesn't create stored procedures automatically. Iron Speed designer
Visual Studio 2003 and 2005 (and perhaps 2008 for all I know) require the
Visual Studio Solution files contain two GUID's per project entry. I figure one of
Visual Studio solutions contain two types of hidden user files. One is the solution
Visual Studio randomly crashes when adding/removing references and projects. Any thoughts why? Will installing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.