开发者

How to generate xml file without the header

开发者 https://www.devze.com 2023-01-25 10:07 出处:网络
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 .OmitX

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

精彩评论

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