1

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?

2 Answers 2

5

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Well, that succeeded in pulling in all of the elements, however it placed them all in one string with no apparent way of separating them. I need the contents of each element to be a separate item in the list. Is this possible?
@BCXTreme that is not the case, the content of each element will be a separate entry in the enumeration, you might just want the list, I will edit that in.
2

This would do it-

 XDocument xmlDoc = XDocument.Load("XMLDocument.xml");
 var ElementsList = from Elements in xmlDoc.Descendants("root")
 select new
 {
   Element1 = (string)Elements.Element("element"),
   Element2 = Elements.Element("element").ElementsAfterSelf("element").First().Value,
   Element3 = Elements.Element("element").ElementsAfterSelf("element").ElementAt(1).Value,
 };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.