How to add a line break when using XmlSerializer

2020-03-01 03:51发布

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

Sample code:

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

2条回答
够拽才男人
2楼-- · 2020-03-01 04:24

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

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

查看更多
戒情不戒烟
3楼-- · 2020-03-01 04:32

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

var xmlWriterSettings = new XmlWriterSettings() { Indent = true };
XmlSerializer serializer = new XmlSerializer(typeof(xxx));
using (XmlWriter xmlWriter = XmlWriter.Create("test.xml", xmlWriterSettings)
{
    serializer.Serialize(xmlWriter, xxx);
}
查看更多
登录 后发表回答