score:2

Accepted answer

Your query is basically selecting the value of your root node <customernumbers>, but the XElement.Value property will return the values of all child nodes put together exactly as you example is showing. What you need to do instead is select each of the child nodes in a query instead.

To use the exact same format of your query, you just need to add one more Elements() method:

var query = from e in xml.Element("customernumbers").Elements()
            select e.Value;

query will now contain a collection of 3 String objects, each with the value of one of your child nodes.

If you have child elements of different names and you only want the value(s) of elements with a specific name, you can change this too:

var query = from e in xml.Element("customernumbers").Elements("customernumber")
            select e.Value;

Now query will contain a collection of the String values of any child element that has the name "customernumber".

You could shorten this up even more:

var query = from e in xml.Descendants("customernumber")
            select e.Value;

This will do the exact same thing. This is much easier when you don't know the exact structure of the XML or it is very complicated. The downside is it will select any child, grandchild, or any other node that has the name "customernumber" regardless of the location within the XML tree, so if you have multiple different nodes with a <customernumber> tag that you do not want to select, this approach will not work correctly.

score:0

Try,

List<string> customerNumbers = (from e in xmlelement.Elements("customernumber")
                    select e.Value).ToList<string>();

score:1

Try this to iterate child elements:

    var query = from e in xml.Element("customernumbers").Elements("customernumber")
                select e.Value;

Related Query

More Query from same tag