Thursday, September 16, 2010

Note on Using LINQ to Query an Atom Feed

More generally, this note is about using LINQ to query XML with one or more namespaces. I ran into the problem of querying XML with one or more namespaces when querying data from Windows Live using the Messenger Connect APIs (this post describes what I was doing). I scratched my head for a while wondering why my LINQ queries didn’t return anything. The XML in question in this example was an Atom feed from a Windows Live endpoint that returns contact information.

In a nutshell, you have to pay attention to what namespaces are defined. My problem was I forgot to consider the default namespace. These two topics on MSDN explain it in more detail: Working with XML Namespaces and Scope of Default Namespaces in C#.

To illustrate the problem precisely, suppose you have the following XML:

XML Listing 1

Then with this C# code you can get the name of the contact.

XDocument xdoc = XDocument.Load(MapPath("~/App_Data/XMLFile.xml"));
var nameQuery1 = xdoc.Descendants("contact");
foreach (XElement nameElement in nameQuery1)
Code Listing 1

Now suppose the XML is defined with a default namespace such as this:

<contacts xmlns="">
XML Listing 2

In this case Code Listing 1 would not return the name element because it doesn’t take in account the default namespace. The following code will work:

XNamespace ns = "";
var nameQuery2 = xdoc.Descendants(ns + "contact");
foreach (XElement nameElement in nameQuery2)
Response.Write(nameElement.Element(ns + "name").Value);
Code Listing 2

Now, suppose there is an additional namespace declared with a prefix defined such as this:

<contacts xmlns="" xmlns:an="http://anothernamespace">
XML Listing 3

Possible C# code to get the note element is:

XNamespace ns2 = "http://anothernamespace";
var nameQuery3 = xdoc.Descendants(ns + "contact");
foreach (XElement nameElement in nameQuery3)
Response.Write(nameElement.Element(ns2 + "note").Value);
Code Listing 3

The code examples above are just for illustration purposes. You should make the code more robust, e.g. using try/catch clauses, for your particular application.

The following XML is an excerpt from the Messenger Connect AllContacts ATOM feed. It is followed by code to read it. It runs in the context of a web page, but the code can be easily taken out and used in other scenarios.

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="" xmlns:xml="" xmlns="">
<title type="text">AllContacts</title>
<link rel="self" type="application/atom+xml;type=feed" title="self" href="Contacts/AllContacts"/>
<link rel="edit" type="application/atom+xml;type=feed" title="edit" href="Contacts/AllContacts"/>
<entry p3:reserved=" " p4:etag="0001-01-01T00:00:00.0000000" xmlns:p4="" xmlns:p3="">
<title type="text">Cosentino Luparello</title>
<link rel="self" type="application/atom+xml;type=entry" title="self" href="Contacts/AllContacts/F2E3AWHYXZYUVHICHHNFXIXKHQ"/>
<link rel="edit" type="application/atom+xml;type=entry" title="edit" href="Contacts/AllContacts/F2E3AWHYXZYUVHICHHNFXIXKHQ"/>
<category term="Contact" label="Contact" scheme=""/>
<content type="application/xml">
<p4:properties xmlns:i="">
<p3:FormattedName>Cosentino Luparello</p3:FormattedName>
XML Listing 4

<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Xml.Linq" %>
<script runat="server">
public class ContactName
public string FirstName { get; set; }
public string LastName { get; set; }
protected void Page_Load(object sender, EventArgs e)
// Atom namespace.
XNamespace ns = "";
// Dataservices namespace, p3.
XNamespace nsp3 = "";
// Dataservices metadata namespace, p4.
XNamespace nsp4 = "";

XDocument xdoc = XDocument.Load(MapPath("~/App_Data/data.xml"));

Output("Query 1: Selecting content node and then iterating on it to find FirstName and LastName.");
// Select the content nodes directly.
var contentQuery = xdoc.Descendants(ns + "content");
// Iterate over the collection of content nodes.
foreach (XElement contentElement in contentQuery)
string firstName = contentElement.Descendants(nsp3 + "FirstName").FirstOrDefault().Value;
string lastName = contentElement.Descendants(nsp3 + "LastName").FirstOrDefault().Value;
Output(firstName + " " + lastName);

Output("Query 2: Selecting FirstName and LastName as string.");
var firstNameQuery = xdoc.Descendants(nsp3 + "FirstName").Select(entry => entry.Value + " " + entry.ElementsAfterSelf(nsp3 + "LastName").First().Value);
foreach (string firstNameEntry in firstNameQuery)

Output("Query 3: Selecting FirstName and LastName as ContactName object.");
var contactNameQuery = xdoc.Descendants(nsp4 + "properties").Select(entry => new ContactName
FirstName = entry.Element(nsp3 + "FirstName").Value,
LastName = entry.Element(nsp3 + "LastName").Value
foreach (var contactNameEntry in contactNameQuery)
Output(contactNameEntry.FirstName + " " + contactNameEntry.LastName);

Output("Query 4: Query 3 + filtering results.");
var contactNameFilteredQuery = xdoc.Descendants(nsp4 + "properties")
.Where(entry => entry.Element(nsp3 + "FirstName").Value.ToLower().StartsWith("c"))
.Select(entry => new ContactName
FirstName = entry.Element(nsp3 + "FirstName").Value,
LastName = entry.Element(nsp3 + "LastName").Value
foreach (var contactNameFilteredEntry in contactNameFilteredQuery)
Output(contactNameFilteredEntry.FirstName + " " + contactNameFilteredEntry.LastName);
protected void Output()
protected void Output(string txt)
Label1.Text += txt + "<br/>";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
<title>Reading Windows Live Contact Atom Feed</title>
<form id="form1" runat="server">
Using LINQ extension methods and lambda expressions.<br />
<asp:Label ID="Label1" runat='server'></asp:Label>
Code Listing 4

1 comment:

  1. I've been Googling and trying to understand how parsing with a namespace works for over 2 hours but failed, thanks for this great article, I've totally understood how it works now.