using System.Xml.Serialization;
public static void Main()
var expectedResult = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<PRODUCT>\r\n <NAME>OhneWert</NAME>\r\n</PRODUCT>";
var poco = new Product{Name = "OhneWert"};
var result = new ProductSerializer().Serialize<Product>(poco);
if(result != expectedResult)
throw new InvalidOperationException("Product xml should not contain nullable integer types");
public class ProductSerializer
public string Serialize<T>(T objectToSerialize)
var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
var serializer = new XmlSerializer(typeof(T));
using var stringWriter = new StringWriter();
using var writer = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };
serializer.Serialize(writer, objectToSerialize, namespaces);
return stringWriter.ToString();
[XmlElement(ElementName = "WERT")]
public int? Wert { get; set; } = null!;
[XmlElement(ElementName = "NAME")]
public string Name { get; set; } = string.Empty;