Ignore namespaces in LINQ to XML
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
Visually, the element is named item, but LINQ to XML sees it as urn:demo:item. So this query does not match anything:
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.
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.
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.
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
LocalNamewhen 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.
Related reading
- Ignoring a class property in Entity Framework 4.1 Code First
- Ignoring a field during .NET JSON serialization; similar to XmlIgnore?
- Ignoring null fields in Json.net
- IHttpActionResult vs async TaskIHttpActionResult
- Iif equivalent in C
- IIS Config Error - This configuration section cannot be used at this path
- Illustrating usage of the volatile keyword in C
- ILMerge Best Practices

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.