Der Standardserializer verwendet UTF-16, will man aber ein XML-File mit UTF8 muss man eine andere Lösung finden.
Der Standard ist folgender:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TestObject));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, _testObject);
string s = stringWriter.ToString();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TestObject));
MemoryStream memStrm = new MemoryStream();
UTF8Encoding utf8e = new UTF8Encoding();
XmlTextWriter xmlSink = new XmlTextWriter(memStrm, utf8e);
xmlSerializer.Serialize(xmlSink, _testObject);
byte[] utf8EncodedData = memStrm.ToArray();
return utf8e.GetString(utf8EncodedData);
Kommentare zum Snippet