<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju" -->

---
title: XPath for custom types in .NET | daily.dev
description: XPath, typically associated with XML documents, can be implemented for any custom .NET type by subclassing XPathNavigator and IXPathNavigable. A worked example...
canonical: https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: XPath for custom types in .NET | daily.dev
og:description: XPath, typically associated with XML documents, can be implemented for any custom .NET type by subclassing XPathNavigator and IXPathNavigable. A worked example...
og:url: https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju
og:image: https://api.daily.dev/og/posts/FgtHF58jU.png
og:image:alt: XPath for custom types in .NET
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# XPath for custom types in .NET

**[Meziantou](https://daily.dev/sources/meziantou)** · 9 min read · 5 upvotes · 0 comments

## Summary

XPath, typically associated with XML documents, can be implemented for any custom .NET type by subclassing XPathNavigator and IXPathNavigable. A worked example builds a navigator for a Folder/FileItem object hierarchy, tracking node position with a linked list and a NodeKind enum to support document root, element, and attribute navigation. The piece also covers exposing custom XPath functions (like has-extension) via XsltContext and IXsltContextFunction, letting predicates run arbitrary logic against object properties. The result lets users query complex object graphs with XPath expressions like //File[@ContentType='image/png'] instead of writing bespoke LINQ search methods.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.meziantou.net/xpath-for-custom-types-in-dotnet.htm>

## Questions this post answers

### How do I make a custom .NET class support XPath queries instead of just XML documents?

Implement IXPathNavigable on the type by adding a CreateNavigator() method that returns a custom XPathNavigator subclass. The navigator must implement NodeType, LocalName, Value, and tree navigation methods like MoveToFirstChild and MoveToNext, mapping your object's properties and children to XPath's node model, including a virtual document root so expressions starting with '/' work.

_Developers building searchable object trees in .NET can save this XPathNavigator pattern on daily.dev._

### Can I add custom functions to XPath expressions in .NET, like a has-extension() predicate?

Yes, by implementing XsltContext to resolve function names and IXsltContextFunction to implement the logic. Compile the expression with XPathExpression.Compile, call SetContext with the custom XsltContext instance, then use nav.Select(expr). The function's Invoke method receives the current node as an XPathNavigator, so it can read attributes or navigate the tree for arbitrary predicate logic.

_Anyone extending XPath predicates in .NET can keep this XsltContext technique handy on daily.dev._

### What interfaces do I need to implement to make a custom XPathNavigator for a non-XML data structure?

Three pieces are required: a class implementing IXPathNavigable as the entry point, a class implementing the abstract XPathNavigator with tree, attribute, and namespace navigation methods (MoveToFirstChild, MoveToNextAttribute, MoveToFirstNamespace, Clone, MoveTo, IsSamePosition, MoveToId), and a way to expose the underlying data through these interfaces, typically tracking position with a linked-list-style NodePosition back to a virtual document root.

_Developers designing custom query layers over object graphs can reference this XPathNavigator breakdown on daily.dev._

## Similar posts on daily.dev

- [Older Tech In The Browser Stack — Smashing Magazine](https://daily.dev/posts/older-tech-in-the-browser-stack-smashing-magazine-pmhqremwo) · Smashing Magazine · 1 upvotes · 0 comments
- [Querying Roslyn syntax trees with XPath](https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar) · Meziantou · 5 upvotes · 1 comments
- [The Many Languages of Data: How .NET Learned to Speak in Formats](https://daily.dev/posts/the-many-languages-of-data-how-net-learned-to-speak-in-formats-56sn8nk5g) · We Are .NET · 41 upvotes · 3 comments
- [Using JsonPath with System.Text.Json to query JSON in .NET](https://daily.dev/posts/using-jsonpath-with-system-text-json-to-query-json-in-net-pd3liujm6) · Meziantou · 10 upvotes · 1 comments
- [XML is a cheap DSL](https://daily.dev/posts/xml-is-a-cheap-dsl-nzs8ti7hg) · Lobsters · 0 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#), [#design-patterns](https://daily.dev/tags/design-patterns)

[View this post on daily.dev](https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"XPath for custom types in .NET","url":"https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju"},"datePublished":"2026-09-14T12:11:01.375Z","dateModified":"2026-09-14T12:11:25.622Z","description":"XPath, typically associated with XML documents, can be implemented for any custom .NET type by subclassing XPathNavigator and IXPathNavigable. A worked example...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0408748b13e85081ea61ccfbad0afd4f?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0408748b13e85081ea61ccfbad0afd4f?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Meziantou","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Meziantou","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/1bdfdd5349654b08ab6fa3f1ac38192c","url":"https://daily.dev/sources/meziantou"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,c#,design-patterns","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Meziantou","item":"https://daily.dev/sources/meziantou"},{"@type":"ListItem","position":3,"name":"XPath for custom types in .NET"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju#faq","mainEntity":[{"@type":"Question","name":"How do I make a custom .NET class support XPath queries instead of just XML documents?","acceptedAnswer":{"@type":"Answer","text":"Implement IXPathNavigable on the type by adding a CreateNavigator() method that returns a custom XPathNavigator subclass. The navigator must implement NodeType, LocalName, Value, and tree navigation methods like MoveToFirstChild and MoveToNext, mapping your object's properties and children to XPath's node model, including a virtual document root so expressions starting with '/' work. Developers building searchable object trees in .NET can save this XPathNavigator pattern on daily.dev."}},{"@type":"Question","name":"Can I add custom functions to XPath expressions in .NET, like a has-extension() predicate?","acceptedAnswer":{"@type":"Answer","text":"Yes, by implementing XsltContext to resolve function names and IXsltContextFunction to implement the logic. Compile the expression with XPathExpression.Compile, call SetContext with the custom XsltContext instance, then use nav.Select(expr). The function's Invoke method receives the current node as an XPathNavigator, so it can read attributes or navigate the tree for arbitrary predicate logic. Anyone extending XPath predicates in .NET can keep this XsltContext technique handy on daily.dev."}},{"@type":"Question","name":"What interfaces do I need to implement to make a custom XPathNavigator for a non-XML data structure?","acceptedAnswer":{"@type":"Answer","text":"Three pieces are required: a class implementing IXPathNavigable as the entry point, a class implementing the abstract XPathNavigator with tree, attribute, and namespace navigation methods (MoveToFirstChild, MoveToNextAttribute, MoveToFirstNamespace, Clone, MoveTo, IsSamePosition, MoveToId), and a way to expose the underlying data through these interfaces, typically tracking position with a linked-list-style NodePosition back to a virtual document root. Developers designing custom query layers over object graphs can reference this XPathNavigator breakdown on daily.dev."}}]}
```

