Skip to content

Instantly share code, notes, and snippets.

@dontpaniclabsgists
Created March 18, 2025 17:23
Show Gist options
  • Save dontpaniclabsgists/891aae1efddec611e86ac82f4675c6da to your computer and use it in GitHub Desktop.
Save dontpaniclabsgists/891aae1efddec611e86ac82f4675c6da to your computer and use it in GitHub Desktop.
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
```
Using this custom writer, we can write our XML in the same way.
```C#
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (Utf8StringWriter writer = new Utf8StringWriter())
{
serializer.Serialize(writer, person, namespaces);
Console.WriteLine(writer.ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment