how to use XPath with XDocument?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- How to use XPath with XElement or LINQ?
- How to verify that method was NOT called in Moq?
- How to view the Folder and Files in GAC?
- How to wait for a async void method to complete its task?
- How to wait for a BackgroundWorker to cancel?
- How to wait for a BackgroundWorker to cancel?
- How to wait for async method to finish in C?
- How to wait until remote .NET debugger attached

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.