我不需要标题。 如何使用xml序列化程序?
答案 0 :(得分:7)
XmlSerializer
对此不负责任 - XmlWriter
,因此,此处的关键是创建一个XmlWriterSettings
对象,其中.OmitXmlDeclaration
设置为 true ,并在构造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);
}
}
}