<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar" -->

---
title: Querying Roslyn syntax trees with XPath | daily.dev
description: A new rule (MA0240) in Meziantou.Analyzer exposes each C# file's Roslyn syntax tree as an XML document, letting teams ban specific language constructs using...
canonical: https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Querying Roslyn syntax trees with XPath | daily.dev
og:description: A new rule (MA0240) in Meziantou.Analyzer exposes each C# file's Roslyn syntax tree as an XML document, letting teams ban specific language constructs using...
og:url: https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar
og:image: https://api.daily.dev/og/posts/31k1Vo1Ar.png
og:image:alt: Querying Roslyn syntax trees with XPath
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.

# Querying Roslyn syntax trees with XPath

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

## Summary

A new rule (MA0240) in Meziantou.Analyzer exposes each C# file's Roslyn syntax tree as an XML document, letting teams ban specific language constructs using XPath 1.0 queries in a BannedSyntaxes.txt file instead of writing dedicated Roslyn analyzers. Syntax nodes become elements named after their SyntaxKind, and tokens become attributes, so queries can use XPath predicates, axes like ancestor::, and functions like count() and contains() for free. Examples include banning primary constructors, nested conditional expressions, goto statements, and methods with too many parameters. Invalid queries are flagged by a companion rule, MA0241, while valid lines in the same file still apply.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.meziantou.net/querying-roslyn-syntax-trees-with-xpath.htm>

## Questions this post answers

### How do I ban primary constructors on classes using Meziantou.Analyzer?

Add a BannedSyntaxes.txt file to your project or repository root with the line //ClassDeclaration/ParameterList; Do not use primary constructors. The MA0240 rule in Meziantou.Analyzer evaluates this XPath 1.0 query against each file's Roslyn syntax tree exposed as XML and reports a warning wherever a class declares a primary constructor.

_daily.dev surfaces practical Roslyn analyzer techniques for teams enforcing C# coding standards._

### How does Meziantou.Analyzer expose a Roslyn C# syntax tree as XML for XPath queries?

Each syntax node becomes an XML element named after its SyntaxKind (like ClassDeclaration or ParameterList), with child nodes as child elements in source order. Each token, such as Identifier or Modifiers, becomes an attribute holding the token's text; missing tokens are simply omitted as attributes, and the document root is the CompilationUnit node.

_track how tools like this simplify Roslyn syntax analysis by following .NET tooling updates on daily.dev._

### What does the MA0241 rule report in Meziantou.Analyzer?

MA0241 reports invalid lines in a BannedSyntaxes.txt file, such as a name that isn't a member of SyntaxKind, an invalid XPath query, or a query that doesn't return a node-set. Other valid lines in the same file continue to be applied, so one typo doesn't silently disable the entire banned-syntax configuration.

_developers debugging custom analyzer configs can keep up with .NET analyzer tooling changes on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@nark3d** · 0 upvotes

> Turning syntax nodes into XML elements and tokens into attributes is a clever bit of reuse, because it means the bans can be written with the XPath everyone already half-remembers instead of learning the analyzer SDK.  Banning primary constructors and goto with a predicate costs a line, while the equivalent analyzer needs a registered action and a diagnostic descriptor.  I do not mind the XPath 1.0 limitation.  ancestor:: and count() will carry most of the weight, and if you need a regex on a node's text, you will have to write that analyzer.  MA0241 flagging broken queries while the rest of...

## Similar posts on daily.dev

- [Build Your First Roslyn Analyzer in C\#: Diagnostics and Code Fix Walkthrough](https://daily.dev/posts/build-your-first-roslyn-analyzer-in-c-diagnostics-and-code-fix-walkthrough-069rztvx7) · We Are .NET · 14 upvotes · 2 comments
- [XPath for custom types in .NET](https://daily.dev/posts/xpath-for-custom-types-in-net-fgthf58ju) · Meziantou · 5 upvotes · 0 comments
- [New features and Roslyn analyzers for Meziantou.Framework.FullPath](https://daily.dev/posts/new-features-and-roslyn-analyzers-for-meziantou-framework-fullpath-po20wwci9) · Meziantou · 6 upvotes · 0 comments
- [Cut your coding agent’s cost with Sonar Vortex](https://daily.dev/posts/cut-your-coding-agent-s-cost-with-sonar-vortex-jlvdo2nwa) · Security Boulevard · 1 upvotes · 0 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar)

```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":"Querying Roslyn syntax trees with XPath","url":"https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar"},"datePublished":"2026-09-20T12:11:42.184Z","dateModified":"2026-09-20T12:12:08.140Z","description":"A new rule (MA0240) in Meziantou.Analyzer exposes each C# file's Roslyn syntax tree as an XML document, letting teams ban specific language constructs using...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1c920603694d6a1d728e2cafbe06cb3a?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1c920603694d6a1d728e2cafbe06cb3a?_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":1,"discussionUrl":"https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":".net,c#","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":"Querying Roslyn syntax trees with XPath"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar","comment":[{"@type":"Comment","text":"Turning syntax nodes into XML elements and tokens into attributes is a clever bit of reuse, because it means the bans can be written with the XPath everyone already half-remembers instead of learning the analyzer SDK.  Banning primary constructors and goto with a predicate costs a line, while the equivalent analyzer needs a registered action and a diagnostic descriptor.  I do not mind the XPath 1.0 limitation.  ancestor:: and count() will carry most of the weight, and if you need a regex on a node’s text, you will have to write that analyzer.  MA0241 flagging broken queries while the rest of the file still applies is hard to get right.  A team needs it: a bad line fails loudly instead of silently switching off every ban around it.","datePublished":"2026-09-28T16:29:25.915Z","url":"https://daily.dev/posts/31k1Vo1Ar#c-Yio9DG5WA","author":{"@type":"Person","name":"Adam","url":"https://daily.dev/nark3d","image":"https://avatars.githubusercontent.com/u/2162621?v=4"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/querying-roslyn-syntax-trees-with-xpath-31k1vo1ar#faq","mainEntity":[{"@type":"Question","name":"How do I ban primary constructors on classes using Meziantou.Analyzer?","acceptedAnswer":{"@type":"Answer","text":"Add a BannedSyntaxes.txt file to your project or repository root with the line //ClassDeclaration/ParameterList; Do not use primary constructors. The MA0240 rule in Meziantou.Analyzer evaluates this XPath 1.0 query against each file's Roslyn syntax tree exposed as XML and reports a warning wherever a class declares a primary constructor. daily.dev surfaces practical Roslyn analyzer techniques for teams enforcing C# coding standards."}},{"@type":"Question","name":"How does Meziantou.Analyzer expose a Roslyn C# syntax tree as XML for XPath queries?","acceptedAnswer":{"@type":"Answer","text":"Each syntax node becomes an XML element named after its SyntaxKind (like ClassDeclaration or ParameterList), with child nodes as child elements in source order. Each token, such as Identifier or Modifiers, becomes an attribute holding the token's text; missing tokens are simply omitted as attributes, and the document root is the CompilationUnit node. track how tools like this simplify Roslyn syntax analysis by following .NET tooling updates on daily.dev."}},{"@type":"Question","name":"What does the MA0241 rule report in Meziantou.Analyzer?","acceptedAnswer":{"@type":"Answer","text":"MA0241 reports invalid lines in a BannedSyntaxes.txt file, such as a name that isn't a member of SyntaxKind, an invalid XPath query, or a query that doesn't return a node-set. Other valid lines in the same file continue to be applied, so one typo doesn't silently disable the entire banned-syntax configuration. developers debugging custom analyzer configs can keep up with .NET analyzer tooling changes on daily.dev."}}]}
```

