如何在使用XmlSerializer时添加换行符
我想知道如何在使用XmlSerializer时为每个元素添加换行符?
示例代码:
XmlSerializer serializer = new XmlSerializer(typeof(xxx)); using (XmlWriter xmlWriter = XmlWriter.Create("test.xml") { serializer.Serialize(xmlWriter, xxx); }
创建XmlWriter
,传入一个将Indent
设置为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); }
您可以使用XmlWriterSettings并将属性设置为缩进和换行符。 .Indent和.NewLineOnAttributes似乎是你想要的。
https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx
上述就是C#学习教程:如何在使用XmlSerializer时添加换行符分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/954748.html