开发者

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);
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜