Skip to content

XML deserialization in C#: retarget net10.0, cache the serializer, read the XML files - #2231

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/94646-csharp-xml-deserialization
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/94646-csharp-xml-deserialization

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the article XML Deserialization in C# (https://code-maze.com/csharp-xml-deserialization/). This PR touches only xml-csharp/XMLDeserializationInCsharp.

Changes:

  • Both projects move from net7.0 to net10.0.
  • Test packages: MSTest.TestAdapter and MSTest.TestFramework 2.2.10 become the single MSTest 4.4.1 package, Microsoft.NET.Test.Sdk 17.5.0 to 18.10.1, coverlet.collector 3.2.0 to 10.0.1. The four existing tests use only Assert.IsNotNull and Assert.AreEqual, so no assertion changes for MSTest 4.
  • Program.cs moves to top-level statements. The Person and Library examples now read person.xml and library.xml with a StreamReader, as the article does. Both files were already copied to the output folder by the project but never opened. The record examples keep their raw strings.
  • Removed the stray using static System.Reflection.Metadata.BlobBuilder; and the duplicated PersonRecord example squashed onto one line. That line was also the only warning the retarget produced (CS8602), so the build is now warning free.
  • DeserializeXmlData moves into its own static class, XmlDeserializer, and the four test call sites follow it.
  • Removed the private LibraryRecord(List) constructor. XmlSerializer never calls it.
  • New PersonSerializer: a static readonly XmlSerializer built with an XmlRootAttribute, matching the article's new section on reusing serializer instances. One new test covers it.

Package versions were re-queried on nuget.org on 2026-09-23, the day this PR opened: MSTest 4.4.1 and Microsoft.NET.Test.Sdk 18.10.1 are newer than the 4.4.0 and 18.10.0 in the batch plan, so the newer ones are used. coverlet.collector 10.0.1 is unchanged.

Verified locally on SDK 10.0.302, runtime 10.0.10: dotnet build -c Release, 0 warnings, 0 errors. dotnet test, 5 passed, 0 failed. dotnet list package --vulnerable --include-transitive reports no vulnerable packages. dotnet run prints both XML files and both record examples.

…ad the XML files

- Retarget both projects from net7.0 to net10.0.
- Collapse MSTest.TestAdapter and MSTest.TestFramework 2.2.10 into MSTest 4.4.1;
  Microsoft.NET.Test.Sdk 18.10.1, coverlet.collector 10.0.1.
- Program.cs moves to top-level statements. The Person and Library examples read
  person.xml and library.xml, which the project already copies to the output folder,
  so the sample runs the same code as the article. The record examples keep their
  raw strings.
- Remove the stray BlobBuilder using directive and the duplicated one-line
  PersonRecord example (it also carried the only CS8602 warning of the retarget).
- DeserializeXmlData<T> moves into its own static class, XmlDeserializer; the four
  test call sites follow it.
- Remove the unreachable private LibraryRecord(List<BookRecord>) constructor.
- Add PersonSerializer, a static readonly XmlSerializer built with an
  XmlRootAttribute, and a test for it.
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