I don't need the header . How to do it with xml serializer?
XmlSerializer
isn't responsible for that - XmlWriter
is, so the key here is to create a XmlWriterSettings
object with .OmitXmlDeclaration
set to true, and pass that in when constructing the XmlWriter
:
using System.Xml;
using System.Xml.Serialization;
public class Foo
{
public string Bar { get; set; }
}
static class Program
{
static void Main()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlWriter.Create("my.xml", settings))
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));
Foo foo = new Foo();
foo.Bar = "abc";
ser.Serialize(writer, foo);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With