Tuesday, September 16, 2008

Remove ?xml version="1.0" encoding="utf-8"? in c#

Hi,
Here is the code to remove xml header

public class XmlTextWriterFormattedNoDeclaration : System.Xml.XmlTextWriter
{
public XmlTextWriterFormattedNoDeclaration(System.IO.TextWriter w): base(w)
{
Formatting = System.Xml.Formatting.Indented;
}

public override void WriteStartDocument() { } // suppress
}
Then you can serialize like this

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer serializer = new XmlSerializer(typeof(object));
StringWriter sw = new StringWriter();
XmlWriter writer = new XmlTextWriterFormattedNoDeclaration(sw);
serializer.Serialize(writer, this, ns);



No comments: