开发者

How to add a line break when using XmlSerializer

开发者 https://www.devze.com 2023-01-29 14:06 出处:网络
I am wondering how to add a line break for each element when usi开发者_StackOverflowng XmlSerializer?

I am wondering how to add a line break for each element when usi开发者_StackOverflowng XmlSerializer?

Sample code:

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


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);
}


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

0

精彩评论

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

关注公众号