score:8
you can do this
xnamespace ns = xnamespace.get("xxxx");
var listofnames = doc.descendants(ns + "feed")
.select(x => x.elements().first().value).tolist();
+1 for lazyberezovsky's answer. if you need to specify the element name (name
in this case) or you could have multiple name elements then you need to add a second namespace for those elements.
xnamespace ns2 = xnamespace.get("xxxxx");
var listofnames = doc.root.descendants(ns2 + "name").select(x => x.value).tolist();
score:4
to get rid of namespaces
in xlinq
queries, use similar method mentioned below:
string xml_string = @"<ns2:feeds xmlns:ns2=""xxxx"" xmlns=""xxxxx"" version=""3.0"">
<ns2:feed>
<name>xxx</name>
</ns2:feed>
<ns2:feed>
<name>yyy</name>
</ns2:feed>
<ns2:feed>
<name>zzz</name>
</ns2:feed>
</ns2:feeds>";
var query = xelement.parse(xml_string).descendants()
.where(c => c.name.localname.tostring() == "name")
.toarray();
foreach (string item in query)
{
console.writeline(item);
}
score:5
with xpathselectelements you should provide namespace manager in order to use namespaces in xpath query:
var manager = new xmlnamespacemanager(new nametable());
manager.addnamespace("ns2", "xxxx");
manager.addnamespace("ns", "xxxxx"); // default namespace
var names = from n in xdoc.xpathselectelements("//ns2:feed/ns:name", manager)
select (string)n;
without xpath you should use xnamespace when providing name of node to find:
xnamespace ns = "xxxxx";
xnamespace ns2 = "xxxx";
var names = from f in xdoc.descendants(ns2 + "feed")
select (string)f.element(ns + "name");
Source: stackoverflow.com
Related Query
- How to parse XML with namespace
- How to parse xml with LINQ to an Object with XDocument
- How do I use LINQ to parse XML with child elements of the same name?
- How to Parse an XML file with Linq Lambda from Given Key
- LINQ to XML how do I get child element value with multiple namespace
- How to parse xml data with dynamic linq
- how to parse this xml with LINQ
- Parse XML string to Object with custom namespace
- How to parse complex XML with attribute as a condition in c#
- How to parse xml file with System.Xml.Linq
- What does this C# code with an "arrow" mean and how is it called?
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- How to parse XML data into the properties of a custom C# class?
- Parsing SOAP response with LINQ to XML - how to get nested nodes under parent?
- How to read XML file using System.IO.Stream with LINQ
- Parse XML with LINQ to get child elements
- Trying to parse XML tree with Linq to XML (C#)
- How to merge 2 XML files with C#
- How do you remove xmlns from elements when generating XML with LINQ?
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- Querying xml child elements with prefixed namespace using LINQ to XML
- How to read XML file to a Dictionary<String,List<String>> with empty strings for null elements
- How to add namespace to xml using linq xml
- Use LINQ XML with a namespace
- How to combine and update two xml files with LINQ c#
- How to query by where clause with EF code first
- how to add(or ignore) XML namespace when using XElement.Load
- Using Linq, how to parse Xml to C# objects which only accept parameters in the constructor?
- How do I represent an option calculated column with EF Code First?
- How can I combine IObservable<T>.Throttle() with some other event source using Reactive Extensions?
More Query from same tag
- Combine 2 lists using linq and add fields for duplicate records
- c# & linq to sql: using datagrid to add multiple records
- Linq: Explicit construction of entity type in query is not allowed
- Verify if a list (or a sublist of that list) of decimal values can equal a certain sum
- action is always return null
- Using Linq to group a list of objects into a new grouped list of list of objects
- Dynamic Expression Methods
- Custom method in IQueryable<T>.Select
- CTE recursive query with linq mvc
- How to get alternate numbers using Enumerable.Range?
- Linq query to order a list based on date and get distinct
- Dynamically Modifying a LINQ to SQL Select Statement's Columns
- Cannot convert source type to target type List<KeyValuePair> Linq
- Using LINQ to build a new collection
- linq xml select node
- How can I achieve SQL CASE statement from LINQ
- EntityFunctions.AsNonUnicode equivalent for .NET Core
- Using list extensions how to check if value exists and I am not out of range?
- SQL: Multiple Where Clause based on multiple filters
- Counting records in C# using LINQ
- LINQ Select and concatenate results from hierarchical model
- improve this Linq with 2 Selects to same table
- Get class name and property name from an expression () => MyClass.Name
- LINQ return all records and groupby on MVC5 Razor View
- How to write an efficient LINQ query when searching by both parent and child entity fields
- read xml file having namespace in root
- How to convert FileInfo into FileInfo[]
- Reading and Processing XML File
- How can I get type double array from List<string>?
- Linq query - ON clause of inner join cannot compare two Guids