Soenneker.Utils.Xml
11.0.1201
Prefix Reserved
dotnet add package Soenneker.Utils.Xml --version 11.0.1201
NuGet\Install-Package Soenneker.Utils.Xml -Version 11.0.1201
<PackageReference Include="Soenneker.Utils.Xml" Version="11.0.1201" />
<PackageVersion Include="Soenneker.Utils.Xml" Version="11.0.1201" />
<PackageReference Include="Soenneker.Utils.Xml" />
paket add Soenneker.Utils.Xml --version 11.0.1201
#r "nuget: Soenneker.Utils.Xml, 11.0.1201"
#:package Soenneker.Utils.Xml@11.0.1201
#addin nuget:?package=Soenneker.Utils.Xml&version=11.0.1201
#tool nuget:?package=Soenneker.Utils.Xml&version=11.0.1201
Soenneker.Utils.Xml
Cached XmlSerializer helpers for strings and streams, with secure reader defaults and optional removal of nil elements.
Installation
dotnet add package Soenneker.Utils.Xml
Serialize to a string
using Soenneker.Utils.Xml;
string? xml = XmlUtil.Serialize(order);
Serialize<T> returns null when the object is null. By default it uses UTF-8, suppresses the serializer's standard namespace declarations, and removes elements marked xsi:nil="true", xsi:nil="1", or the equivalent unqualified nil attribute.
Removing nil elements is a structural transformation: the entire marked element is removed, not converted to an empty element. It requires an XDocument parse-and-save pass. Set removeXsiNilElements: false for direct streaming and standard XmlSerializer nil output.
string? xml = XmlUtil.Serialize(
order,
encoding: Encoding.UTF8,
removeNamespaces: false,
removeXsiNilElements: false);
removeNamespaces controls the namespace declarations supplied to XmlSerializer; it does not strip namespaces required by attributes or type metadata.
Serialize to a stream
await using var destination = File.Create("order.xml");
XmlUtil.Serialize(
order,
destination,
leaveOpen: true);
Serialization begins at the stream's current position. The utility does not rewind or truncate an existing destination. With the default leaveOpen: false, the destination is closed after a non-null object is serialized. A null object is a no-op.
Pass an IMemoryStreamUtil to use its pooled stream for temporary serialization. The caller retains ownership of the utility; temporary streams obtained from it are disposed by XmlUtil.
Deserialize
Order? fromString = XmlUtil.Deserialize<Order>(xml);
Order? fromStream = XmlUtil.Deserialize<Order>(stream, leaveOpen: true);
Null or empty input returns default. Stream deserialization starts at the current position and returns default when a seekable stream has no remaining bytes. XML declarations determine stream encoding automatically.
DTD processing is prohibited and external XML resolution is disabled. Malformed XML, schema/type mismatches, and serializer errors propagate. The utility does not impose a document-size limit, so constrain untrusted input before deserializing when resource exhaustion is a concern.
XmlSerializer instances are cached per closed generic type. Call the static methods directly; no dependency-injection registration is required.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Soenneker.Utils.MemoryStream (>= 11.0.1627)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Soenneker.Utils.Xml:
| Package | Downloads |
|---|---|
|
Soenneker.Extensions.HttpResponseMessage
A collection of helpful HttpResponseMessage extension methods |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 11.0.1201 | 1,630 | 11/19/2026 |
| 11.0.1200 | 96 | 11/18/2026 |
| 11.0.1199 | 628 | 11/18/2026 |
| 11.0.1197 | 735 | 11/18/2026 |
| 11.0.1196 | 1,156 | 11/15/2026 |
| 11.0.1194 | 2,435 | 11/11/2026 |
| 11.0.1193 | 337 | 11/11/2026 |
| 11.0.1192 | 301 | 11/11/2026 |
| 11.0.1191 | 485 | 11/11/2026 |
| 11.0.1190 | 106 | 11/11/2026 |
| 11.0.1189 | 434 | 11/11/2026 |
| 11.0.1188 | 294 | 11/10/2026 |
| 11.0.1187 | 2,416 | 11/3/2026 |
| 11.0.1186 | 1,044 | 11/3/2026 |
| 11.0.1185 | 641 | 11/3/2026 |
| 11.0.1184 | 451 | 11/2/2026 |
| 11.0.1183 | 2,151 | 10/28/2026 |
| 11.0.1182 | 1,354 | 10/26/2026 |
| 11.0.1181 | 103 | 10/26/2026 |
| 11.0.1180 | 264 | 10/26/2026 |
Updated Soenneker.Utils.MemoryStream to 11.0.1627