如何在使用XmlSerializer时添加换行

时间:2022-01-20 08:35:49

I am wondering how to add a line break for each element when using XmlSerializer?

我想知道如何在使用XmlSerializer时为每个元素添加一个换行符?

Sample code:

示例代码:

XmlSerializer serializer = new XmlSerializer(typeof(xxx));
using (XmlWriter xmlWriter = XmlWriter.Create("test.xml")
{
    serializer.Serialize(xmlWriter, xxx);
}

2 个解决方案

#1


27  

When creating the XmlWriter, pass in an XmlWriterSettings object with Indent set to true.

在创建XmlWriter时,传入一个将缩进设置为true的XmlWriterSettings对象。

var xmlWriterSettings = new XmlWriterSettings() { Indent = true };
XmlSerializer serializer = new XmlSerializer(typeof(xxx));
using (XmlWriter xmlWriter = XmlWriter.Create("test.xml", xmlWriterSettings)
{
    serializer.Serialize(xmlWriter, xxx);
}

#2


6  

You can use XmlWriterSettings and set the properties to out the indentation and newlines. .Indent and .NewLineOnAttributes seem to be what you would want.

您可以使用XmlWriterSettings并将属性设置为缩进和换行。

http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx

http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx

#1


27  

When creating the XmlWriter, pass in an XmlWriterSettings object with Indent set to true.

在创建XmlWriter时,传入一个将缩进设置为true的XmlWriterSettings对象。

var xmlWriterSettings = new XmlWriterSettings() { Indent = true };
XmlSerializer serializer = new XmlSerializer(typeof(xxx));
using (XmlWriter xmlWriter = XmlWriter.Create("test.xml", xmlWriterSettings)
{
    serializer.Serialize(xmlWriter, xxx);
}

#2


6  

You can use XmlWriterSettings and set the properties to out the indentation and newlines. .Indent and .NewLineOnAttributes seem to be what you would want.

您可以使用XmlWriterSettings并将属性设置为缩进和换行。

http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx

http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx