how to use XPath with XDocument?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
XDocument belongs to LINQ to XML, which means its most natural query style is LINQ. Even so, you can still use XPath against an XDocument in .NET by importing the XPath extension methods from System.Xml.XPath.
The Required Namespace
The most common mistake is loading an XDocument correctly but forgetting the namespace that adds XPath extension methods.
Once System.Xml.XPath is imported, you can call methods such as XPathSelectElement, XPathSelectElements, and XPathEvaluate.
Basic Example
Here is a simple query that finds all book titles from an XML document:
This is useful when you already know the XPath expression you want and do not want to rewrite it as LINQ.
Selecting a Single Element
If you expect exactly one match, XPathSelectElement is simpler:
The XPath predicate filters on the category attribute. This is one of the big reasons people still reach for XPath: compact filtering syntax.
Working with XML Namespaces
Namespace-aware XML requires extra setup. A plain XPath query usually fails if the XML uses namespaces and you ignore them.
Without the namespace manager, the query returns no match even though the element obviously exists.
XPath vs LINQ to XML
XPath is great when:
- you already have existing XPath expressions
- the query is easier to read in XPath form
- you need compact attribute-based filtering
LINQ to XML is often better when:
- you want strong C# typing and composability
- the query logic is built dynamically in code
- you prefer native LINQ transformations over string expressions
Both are valid. The main thing is to choose one style intentionally instead of mixing them randomly.
If you inherited XML queries from another system or an older codebase, keeping them in XPath can also reduce translation mistakes. In contrast, if the query is being built by new C# code from scratch, LINQ to XML is often easier to refactor safely.
You can also use XPathEvaluate when the result is not a simple element selection, such as a count or a boolean expression. That makes XPath useful for quick checks as well as node retrieval.
For example, an existence check can be easier to express in XPath than in a larger LINQ projection when you only need a yes-or-no answer from the XML.
Common Pitfalls
- Forgetting
using System.Xml.XPath;, which makes the extension methods appear to be missing. - Writing namespace-unaware XPath against namespaced XML.
- Expecting XPath results to be strongly typed beyond
XElement,IEnumerable<XElement>, or general evaluation objects. - Using XPath for everything when a straightforward LINQ to XML query would be clearer in C#.
Summary
- '
XDocumentcan use XPath through extension methods inSystem.Xml.XPath.' - '
XPathSelectElementandXPathSelectElementsare the most common entry points.' - XPath works well for compact XML filtering expressions.
- Namespaced XML requires an
XmlNamespaceManager. - LINQ to XML is often more idiomatic, but XPath is still useful when it fits the query better.

