score:2
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:1
Try this to iterate child elements:
var query = from e in xml.Element("customernumbers").Elements("customernumber")
select e.Value;
score:0
Try,
List<string> customerNumbers = (from e in xmlelement.Elements("customernumber")
select e.Value).ToList<string>();
Source: stackoverflow.com
Related Articles
- How to parse multiple XML elements which are not unique
- Parse XML with Linq with multiple child elements
- Querying an XML document which has multiple elements with same name and with different attribute values
- update multiple elements at once LINQ
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- c# linq - get elements from array which do not exist in a different array
- Select Multiple elements in a row using Linq
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to reuse a linq expression for 'Where' when using multiple source tables
- Parse XML with LINQ to get child elements
- LINQ Source Code Available
- multiple orderby in this linq code
- Using SequenceEqual and then returning which elements don't match
- Getting distinct and ordered members from a list of strings - linq or hashset for unique which one is faster / better suited
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- .NET 4 Code Contracts: "requires unproven: source != null"
- How do I identify which Linq functions will not call to sql multiple times?
- Finding objects which contains at least all elements from subset using RavenDB and LINQ
- LINQ: single list which consists of multiple properties of same type of object collection
- indices of elements which satisfy a given condition
- Using Linq, how to parse Xml to C# objects which only accept parameters in the constructor?
- c# linq select distinct of elements with multiple attribute
- creating Linq to sqlite dbml from DbLinq source code
- How to check multiple attributes having unique combination within List where attribute names are available as separate collection. [C#]
- C# Linq XML Query where multiple elements of same name exist
- Conditionally sorting elements by multiple properties in multiple tables with LINQ
- get attribute value from multiple elements with same name and attribute value of one other element in xml
- Fluent LINQ query with multiple join conditions, one of which being a simple equals and another a less than comparison between ids
- Check which elements are on one list comparing to another list LINQ
- Group list entries with LINQ
- Why Entity Framework Code First one to many Doesn't work properly
- How to Union List<List<String>> in C#
- Dynamic Expression API (Dynamic.cs) Not properly parsing expression in .net 3.5
- Checking if a list is empty with LINQ
- Operator '||' cannot be applied to operands of type 'string' and 'bool'
- Need to create Expression<Action> from strings
- What's the most (performance) efficient and readable way to 'split' a generic list based on a condition?
- LINQ to Entities group by and Count()
- Convert my value type to nullable equivalent
- Combine two Dictionaries with LINQ
- Can I sensibly use an Expression without a collection?
- Linq query list of blob, memory usage
- LinQ DataTable Group By
- Speed Up Performance - LINQ to get items in one LIST, that are not in another List
- is there a way to dynamically assign type to a generic linq expression?
- How can I find the entries with dates older then a specified date in a list but all data types are string?
- what would be the equivalent this for loop in linq
- Why no exception occur on a wrong LINQ query in a task (query never ends in a task)
- How can I use Linq to get correctly group these results?