XNamespace problem when parsing xml with Linq

This might be useful (it  got my head twisted at least) to somebody I hope.
I wanted to parse the elements file in a feature to get all the fields and make a constants class from the output. Easy enough, I had done that numerous times before (parsing xml with Linq).
This time I got in to some problems though. Like to share my experiences with you on that subject.

My elements file looks like this:


For the sake of simplicity (and for this demo in a console app) I´m embedding this xml file as a resource (in a live scenario you would get that data from the 12 hive).
My code for parsing the xml looks like this:


var fields = from f in XElement.Parse(Resources.fields).Descendants("Field")
             select new
                 Name = f.Attribute("Name").Value,
                 Id = f.Attribute("ID").Value

Console.WriteLine("Number of fields parsed: {0}", fields.Count().ToString());

foreach (var item in fields)
    Console.WriteLine("Field name='{0}' id='{1}'", item.Name, item.Id);


So, what am I doing here?
From the parsed xml file I would like to get all elements (nodes) that has the name Field. From that collection I´m selecting the Name and ID attribute values and placing them in an anonymous type. Then doing some prints (which in my “real” code would have written a C# class with the values”).

What?? This can´t be right, I get nothing.

After some digging I removed the namespace from the the xml (the xmlns). That made my code work so after some more digging I found a thing called XNamespace.
From MSDN:

“Every XName contains an XNamespace. Even if an element is not in a namespace, the element’s XName still contains a namespace, XNamespace..::.None. The XName..::.Namespace property is guaranteed to not be null).”

Ok, so apparently I have to declare the XNamespace otherwise my code´s using XNamespace.None which won´t work since my xml is in fact using a namespace.

The modified code looks like this (just showing the modified code, the rest looks the same):

XNamespace xmlns = "http://schemas.microsoft.com/sharepoint/";

var fields = from f in XElement.Parse(Resources.fields).Descendants(xmlns.GetName("Field"))

Notice that I now use xmlns.GetName inside the Descendants method.
You can also write it like this (but I kinda like the GetName thing, makes it more apparent I think).

Descendants(xmlns + "Field")

And the output:



, ,

  1. #1 by Tom on March 11, 2010 - 15:34

    Your images aren’t working… which is a shame, because this is the problem I’m having!

    • #2 by Johan Leino on March 11, 2010 - 15:57

      What do you mean? The images on the site or the javascript that renders the code?

      • #3 by Johan Leino on March 11, 2010 - 15:58

        I can see them both though…

      • #4 by fasdfs@dsfsad.com on April 10, 2014 - 16:38

        The images don’t work. Doesn’t matter which way.

      • #5 by Johan Leino on April 10, 2014 - 17:28

        Yeah…sorry. those images resided on live spaces before i migrated the blog to WordPress. Sadly they are now gone 😦

  2. #6 by Bahadır EKİCİ on June 2, 2012 - 13:27

    Thx your help :))

  3. #7 by toto on December 11, 2014 - 19:35

    thank you, you saved me hours of pain

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: