Edit

Share via


How to debug empty query results sets (LINQ to XML)

One of the most common problems when querying XML trees is that if the XML tree has a default namespace, the developer sometimes writes the query as though the XML were not in a namespace.

The first set of examples in this article shows a typical way that XML in a default namespace is loaded, and then queried improperly.

The second set of examples show the necessary corrections so that you can query XML in a namespace.

For more information, see Namespaces overview.

Example: An improper query on XML in a namespace

This example shows creation of XML in a namespace, and a query that returns an empty result set.

C#
XElement root = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
    <Child>1</Child>
    <Child>2</Child>
    <Child>3</Child>
    <AnotherChild>4</AnotherChild>
    <AnotherChild>5</AnotherChild>
    <AnotherChild>6</AnotherChild>
</Root>");
IEnumerable<XElement> c1 =
    from el in root.Elements("Child")
    select el;
Console.WriteLine("Result set follows:");
foreach (XElement el in c1)
    Console.WriteLine((int)el);
Console.WriteLine("End of result set");

The example produces this result:

Output
Result set follows:
End of result set

Example: A proper query on XML in a namespace

This example shows creation of XML in a namespace, and a query that's coded properly.

The solution is to declare and initialize an XNamespace object, and to use it when specifying XName objects. In this case, the argument to the Elements method is an XName object.

C#
XElement root = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
    <Child>1</Child>
    <Child>2</Child>
    <Child>3</Child>
    <AnotherChild>4</AnotherChild>
    <AnotherChild>5</AnotherChild>
    <AnotherChild>6</AnotherChild>
</Root>");
XNamespace aw = "http://www.adventure-works.com";
IEnumerable<XElement> c1 =
    from el in root.Elements(aw + "Child")
    select el;
Console.WriteLine("Result set follows:");
foreach (XElement el in c1)
    Console.WriteLine((int)el);
Console.WriteLine("End of result set");

The example produces this result:

Output
Result set follows:
1
2
3
End of result set

See also