Search code examples
c#parsinglinq-to-xmlxml-namespacesxelement

Why does XElement fall over when parsing an xml file with an xmlns?


So I'm trying to parse an xml file:

 <?xml version="1.0" encoding="utf-8" ?>
<Root>    
  <att1 name="bob" age="unspecified" xmlns="http://foo.co.uk/nan">    
  </att1>    
</Root>

Using the following code:

XElement xDoc= XElement.Load(filename);
var query = from c in xDoc.Descendants("att1").Attributes() select c;
foreach (XAttribute a in query)
{
    Console.WriteLine("{0}, {1}",a.Name,a.Value);
}

Nothing is written to the console unless I delete xmlns="http://foo.co.uk/nan" from the xml file, after which, I get a list of attribute names and values as one would expect, and as I need!

Edit: Formatting.


Solution

  • You have to use the same namespace in your code:

    XElement xDoc= XElement.Load(filename);
    XNamespace ns = "http://foo.co.uk/nan";
    var query = from c in xDoc.Descendants(ns + "att1").Attributes() select c;
    foreach (XAttribute a in query)
    {
        Console.WriteLine("{0}, {1}",a.Name,a.Value);
    }
    

    Attributes don't pick up the default (xmlns=....) namespace, so you don't need to qualify them. The namespace tag (xmln:tags=....)is purely local to the document or API use, the names are really namespace + local name always so you have to always specify the namespace.