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: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");
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);
}
Source: stackoverflow.com
Related Articles
- How to parse XML with namespace
- Parse XML string to Object with custom namespace
- What does this C# code with an "arrow" mean and how is it called?
- Why doesn't this code compile in VS2010 with .NET 4.0?
- Why is this code with PredicateBuilder not working?
- Parse XML with LINQ to get child elements
- Trying to parse XML tree with Linq to XML (C#)
- System.Linq namespace missing even with reference to System.Core.Dll
- Can you advise me a resource with LINQ/lambda code exercises?
- LINQ Source Code Available
- Linq with where clause in many-to-many EF Code First object
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- .NET 4 Code Contracts: "requires unproven: source != null"
- C#: Adding Columns To Bound DatagridView With Code
- Querying xml child elements with prefixed namespace using LINQ to XML
- Use LINQ XML with a namespace
- Parse youtube feeds with linq
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- How to query by where clause with EF code first
- C# - Linq optimize code with List and Where clause
- compressing code with C# generics
- 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?
- creating Linq to sqlite dbml from DbLinq source code
- How to parse xml with LINQ to an Object with XDocument
- Parse XML in List<CustomClass> with Linq query
- Stubbing Code for Test With Linq Expressions and Lambdas
- Reuse Linq to SQL code with entityframework
- How do I parse a log from a game with regex and/or linq?
- Replacing loops with linq code
- Resharper reworked a for loop. Is this LINQ statement doing too much in one line?
- Linq.Dynamic FirstOrDefault() nested in OrderBy
- Selecting a new object without grouping in linq?
- How to get selected full cell values and compare with database row cell values and update the particular row with new value?
- How to use DateTime.Parse() method both side (i.e in expression left and expression right ) while creating expression tree?
- Linq query & generics
- How to transform this foreach to LINQ
- LINQ Query based on values in a list?
- Put LINQ Query in Method with Property as Parameter
- What alternatives does Java provide for C# expressions?
- Better Code in LinQ?
- How to return Linq expression as a List?
- For loop in a view
- Linq Query for Corresponding SQL
- How to bind XAttribute Value by Foreach
- How do I query only a single item from a database using LINQ?
- C#'s LINQ and .NET Framework, which one depends on the other?
- If Participating in Queryable c# join
- How extract properties from several objects with LINQ
- Method not supported in LINQ Store expression