Skip to content

Selecting XML nodes with XPath: retarget net10.0, drop null-forgiving operators, add LINQ to XML XPath method - #2199

Merged
vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/96192-csharp-selecting-xml-nodes-with-xpath
Sep 20, 2026
Merged

vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/96192-csharp-selecting-xml-nodes-with-xpath

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the sample for the article "Selecting Xml Nodes With XPath in C#".

  • Retargets both projects from net7.0 (out of support since 14 May 2024) to net10.0.
  • Test packages: Microsoft.NET.Test.Sdk 18.10.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1.
  • Program.cs loads the catalog through Path.Combine(AppContext.BaseDirectory, "BooksCatalog.xml"), so "dotnet run" works from the project folder instead of throwing FileNotFoundException. It also prints a message rather than an empty line when the single-node query finds nothing.
  • XmlNodesSelector.cs drops all four null-forgiving (!) operators: SelectSingleBook() returns string? and null-checks, the two list methods return [] when SelectNodes() returns null.
  • Adds SelectBooksWithLinqToXml(XDocument) using XPathSelectElements(), the LINQ to XML equivalent of SelectBooks().
  • Four new tests: SelectSingleNode returns null on no match, SelectNodes returns an empty list on no match, an unprefixed expression returns 3 of the 4 books (the fourth carries a default namespace), and the LINQ to XML method returns the same books as SelectNodes().
  • The three existing assertions compare multi-line XElement.ToString() output against raw string literals, so they call ReplaceLineEndings() on both sides. Without it the tests fail on a clone where git leaves the LF blob unconverted while XElement.ToString() emits CRLF.

Build and tests on SDK 10.0.302 / runtime 10.0.10: 0 warnings, 0 errors, 7/7 passing.

@vladimir-pecanac-main
vladimir-pecanac-main merged commit 69842fc into CodeMazeBlog:main Sep 20, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant