LINQ to XML
XML namespaces
XML parsing
programming
C#

Ignore namespaces in LINQ to XML

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

LINQ to XML treats an element name as the combination of namespace URI and local name. That is why queries such as Descendants("item") often return nothing when the document uses a default namespace. If your requirement is to ignore namespaces intentionally, you need to query by LocalName or normalize the XML first.

Why Namespace-Blind Queries Fail

Consider this XML:

xml
1<root xmlns="urn:demo">
2  <item id="1">A</item>
3  <item id="2">B</item>
4</root>

Visually, the element is named item, but LINQ to XML sees it as urn:demo:item. So this query does not match anything:

csharp
1using System.Xml.Linq;
2using System.Linq;
3
4var doc = XDocument.Parse(xml);
5var items = doc.Descendants("item");
6Console.WriteLine(items.Count());

If the XML contract is stable, the strict fix is to use XNamespace. But when the actual goal is to ignore namespaces, you need a different approach.

Match by LocalName

The simplest namespace-agnostic approach is to inspect Name.LocalName.

csharp
1using System;
2using System.Linq;
3using System.Xml.Linq;
4
5var xml = @"<root xmlns='urn:demo'>
6              <item id='1'>A</item>
7              <item id='2'>B</item>
8            </root>";
9
10var doc = XDocument.Parse(xml);
11
12var items = doc.Descendants()
13    .Where(e => e.Name.LocalName == "item")
14    .ToList();
15
16foreach (var item in items)
17{
18    Console.WriteLine(item.Value);
19}

This works regardless of whether the source used a default namespace or explicit prefixes.

Apply the Same Idea to Attributes

Attributes may be namespaced too. If you decide to ignore namespaces for elements, be consistent when reading attributes.

csharp
1using System;
2using System.Linq;
3using System.Xml.Linq;
4
5var xml = @"<root xmlns='urn:demo'>
6              <item id='1'>A</item>
7            </root>";
8
9var doc = XDocument.Parse(xml);
10var item = doc.Descendants().First(e => e.Name.LocalName == "item");
11
12var id = item.Attributes()
13    .FirstOrDefault(a => a.Name.LocalName == "id")?
14    .Value;
15
16Console.WriteLine(id);

If you ignore element namespaces but still query attributes by full name, the code can become confusing very quickly.

Strip Namespaces into a New Document

If you need to run many namespace-agnostic queries, creating a stripped copy can make later code simpler.

csharp
1using System;
2using System.Linq;
3using System.Xml.Linq;
4
5static XElement StripNamespaces(XElement element)
6{
7    return new XElement(
8        element.Name.LocalName,
9        element.Attributes()
10            .Where(a => !a.IsNamespaceDeclaration)
11            .Select(a => new XAttribute(a.Name.LocalName, a.Value)),
12        element.Nodes().Select(n => n is XElement child ? StripNamespaces(child) : n)
13    );
14}
15
16var original = XDocument.Parse(xml);
17var stripped = new XDocument(StripNamespaces(original.Root!));
18
19Console.WriteLine(stripped.Root?.Element("item")?.Value);

This approach is helpful when upstream XML is inconsistent and you want a normalization step up front.

Know When Not to Ignore Namespaces

Ignoring namespaces is a tolerance technique, not always the best design. Namespaces exist to distinguish vocabularies. If you remove them blindly, different XML families can become indistinguishable.

So the real decision is whether you are consuming unstable third-party XML and want resilience, or whether you control a strict XML contract and want precision. In the second case, explicit namespace-aware queries are usually better.

Keep the Original Document if Namespaces Matter Elsewhere

If other parts of the application still care about namespaces, do not destructively strip them from the one shared document object. Normalize into a copy instead. That keeps the tolerant query path from destroying information needed by stricter parsing paths.

Common Pitfalls

  • Assuming the visible tag text is the full name LINQ to XML uses for matching.
  • Ignoring namespaces for elements but forgetting that attributes can be namespaced too.
  • Stripping namespaces destructively and losing information another part of the application needs.
  • Using namespace-blind queries when the XML contract is actually stable and strict matching would be safer.
  • Mixing strict and tolerant query styles in the same code path without documenting why.

Summary

  • LINQ to XML matches fully qualified names, not just visible tag text.
  • Use LocalName when you intentionally want namespace-agnostic matching.
  • Apply the same idea to attributes if needed.
  • Create a stripped copy when many tolerant queries are required.
  • Prefer explicit namespaces when correctness against a known XML contract matters more than flexibility.

Course illustration
Course illustration

All Rights Reserved.