How to generate xml file without the header
I don't need the header . How to do it wi开发者_运维问答th 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);
}
}
}
精彩评论