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: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;
Source: stackoverflow.com
Related Query
- How to parse multiple XML elements which are not unique
- How to get multiple XML elements that are descendants of another element
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- How do I use Linq to find the elements of a list that are not present in another list?
- linq - how do you do a query for items in one query source that are not in another one?
- How do I identify which Linq functions will not call to sql multiple times?
- Using Linq, how to parse Xml to C# objects which only accept parameters in the constructor?
- How to check multiple attributes having unique combination within List where attribute names are available as separate collection. [C#]
- How to check that which properties/entries are modified and which are not on Editing a record in EF 6
- How to get the list elements that are not into another list C#
- How do I use LINQ to parse XML with child elements of the same name?
- How to get multiple elements by name in XML using LINQ
- Parse XML with Linq with multiple child elements
- C# XML to LINQ multiple elements with same name, how to get them to save in same object
- How to retrieve column names which are byte and not null in C# json web service using LINQ class?
- C# LINQ code not working for XML parse
- How to find specific Elements in Lists which are part of Lists
- Group by on multiple columns and trace values which are not distinct
- how to take desired value from the multiple same elements in the xml by using LInq c#
- Querying an XML document which has multiple elements with same name and with different attribute values
- Return Elements that are not in the other XML
- How to use Linq To XML to get multiple elements and store them differently?
- How to parse nested elements using LINQ to XML
- How are people unit testing code that uses Linq to SQL
- How to get elements by name in XML using LINQ
- c# linq - get elements from array which do not exist in a different array
- How to parse XML data into the properties of a custom C# class?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to check if all the elements in an array are positive integers?
- How can I add elements from one collection that do not exist in a second collection to a third collection?
More Query from same tag
- Adding/summing two arrays
- How do I check the values retrieved from this Linq query as .ToLower()?
- How do dynamically set group by properties and aggregates in a LINQ query?
- How store different data type in list c#
- Using Linq to XML instead of XML Reader/Writer
- Entity Framework Select with Include + Group By Count + Paging and Projection
- Search through MongoDB collections with LINQ which are built dynamically
- Selecting ListBox values against a List
- Linq group by DateTime / intervals
- Using LINQ to order or sort list and output into list box
- 'AnonymousType#1[]' to 'System.Collections.Generic.List<QuestHub.Class1>
- Linq query to get all values from between two dates
- Pass Func(Of TElement, TKey) as a function parameter
- Why is LINQ faster in this example
- Creating a sublist from a parent list
- How can I make a Linq extension for objects with a specific attribute?
- Query works in MSSQL, but not in LINQ
- can the popular javascript tree views (jstree, dynatree...) be setup to read data from an adjacency model?
- How to return a date or an error? c# .Net
- Linq Join on Table with Custom List Issue?
- Aggregate pairs in linq query
- LINQ selecting multiple items stored types
- Difference between using .First() and .Where().First()
- MVC Entity Framework Count
- simplify for iteration to linq query
- Is it possible to get a single element from XMl with the lowest value?
- List<T> vs IEnumerable<T> in foreach
- join two datatable and return the result in another data table
- Sorting Issue with User defined datatypes (Custom Properties) in C#
- Check if string exists in list of strings in C#