How to: Debug Empty Query Results Sets
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 topic shows a typical way that XML in a default namespace is loaded, and is queried improperly.
The second set of examples show the necessary corrections so that you can query XML in a namespace.
For more information, see Working with XML Namespaces.
Example
This example shows creation of XML in a namespace, and a query that returns an empty result set.
XElement root = XElement.Parse(
@"<Root xmlns='https://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");
Dim root As XElement = _
<Root xmlns='https://www.adventure-works.com'>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<AnotherChild>4</AnotherChild>
<AnotherChild>5</AnotherChild>
<AnotherChild>6</AnotherChild>
</Root>
Dim c1 As IEnumerable(Of XElement) = _
From el In root.<Child> _
Select el
Console.WriteLine("Result set follows:")
For Each el As XElement In c1
Console.WriteLine(el.Value)
Next
Console.WriteLine("End of result set")
This example produces the following result:
Result set follows:
End of result set
This example shows creation of XML in a namespace, and a query that is coded properly.
The solution when using C# 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.
The Visual Basic solution is to declare and initialize a global default namespace. This places all XML properties in the default namespace. No other modifications are required to the example to make it work properly.
XElement root = XElement.Parse(
@"<Root xmlns='https://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 = "https://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");
Imports <xmlns="https://www.adventure-works.com">
Module Module1
Sub Main()
Dim root As XElement = _
<Root xmlns='https://www.adventure-works.com'>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<AnotherChild>4</AnotherChild>
<AnotherChild>5</AnotherChild>
<AnotherChild>6</AnotherChild>
</Root>
Dim c1 As IEnumerable(Of XElement) = _
From el In root.<Child> _
Select el
Console.WriteLine("Result set follows:")
For Each el As XElement In c1
Console.WriteLine(CInt(el))
Next
Console.WriteLine("End of result set")
End Sub
End Module
This example produces the following result:
Result set follows:
1
2
3
End of result set