且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

防止 XmlSerializer 格式化输出

更新时间:2022-11-30 14:03:08

不是很直观,但是 XmlWriterSettings 上的 Indent 属性控制了整个格式:

Not very intuitive, but the Indent property on the XmlWriterSettings controls the whole formatting:

var serializer = new XmlSerializer(typeof(MyClass));

using (var writer = new StreamWriter("file.path"))
using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = false }))
{
    serializer.Serialize(xmlWriter, myObject);
}

XmlWriterSettings 您可能想要探索.

There are a few more options on XmlWriterSettings that you might want to explore.