I need to get plain xml, without the <?xml version="1.0" encoding="utf-16"?>
at the beginning and xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
in first element from XmlSerializer
. How can I do it?
Asked
Active
Viewed 7.5k times
114

alexandrul
- 12,856
- 13
- 72
- 99

Grzenio
- 35,875
- 47
- 158
- 240
4 Answers
264
To put this all together - this works perfectly for me:
// To Clean XML
public string SerializeToString<T>(T value)
{
var emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var serializer = new XmlSerializer(value.GetType());
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
using (var stream = new StringWriter())
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, value, emptyNamespaces);
return stream.ToString();
}
}

Wai Ha Lee
- 8,598
- 83
- 57
- 92

Simon Sanderson
- 2,939
- 2
- 19
- 11
-
2using (var stream = new StringWriter()) can be changed to var stream = new StringWriter(); Gives error with code analysis as it tres to dispose xmlwriter twice. – Archna Nov 03 '16 at 21:47
-
1@Archna If you did that, the StringWriter would not be disposed in the case that the XmlWriter.Create call throws an exception. A possible solution that covers malicious XmlWriter authors making an IDispose implementation that does not conform to the guarantee that executing Dispose twice does nothing for the second call would involve a try catch and setting stream to null inside the `using( writer )`, as can be seen in this question: https://stackoverflow.com/a/11192524/2144408. – TamaMcGlinn Oct 18 '18 at 10:26
-
1What are you using the type-parameter T for? – Jesper Dec 11 '19 at 18:01
-
So the key here is `settings.OmitXmlDeclaration = true;`. See [XmlWriterSettings.OmitXmlDeclaration Property](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlwritersettings.omitxmldeclaration?view=net-7.0) – kkuilla Oct 25 '22 at 09:52
27
Use the XmlSerializer.Serialize
method overload where you can specify custom namespaces and pass there this.
var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
serializer.Serialize(xmlWriter, objectToSerialze, emptyNs);
passing null or empty array won't do the trick

kossib
- 480
- 3
- 4
-
8Please note that you need to combine this answer with @tobsen's answer to get what I was asking for - a really clean xml! – Grzenio Nov 23 '09 at 11:00
16
You can use XmlWriterSettings and set the property OmitXmlDeclaration to true as described in the msdn. Then use the XmlSerializer.Serialize(xmlWriter, objectToSerialize) as described here.
2
This will write the XML to a file instead of a string. Object ticket is the object that I am serializing.
Namespaces used:
using System.Xml;
using System.Xml.Serialization;
Code:
XmlSerializerNamespaces emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
XmlSerializer serializer = new XmlSerializer(typeof(ticket));
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true
};
using (XmlWriter xmlWriter = XmlWriter.Create(fullPathFileName, settings))
{
serializer.Serialize(xmlWriter, ticket, emptyNamespaces);
}

Keith Aymar
- 876
- 7
- 10