Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a line break when using XmlSerializer

Tags:

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); } 
like image 796
user496949 Avatar asked Dec 06 '10 06:12

user496949


2 Answers

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); } 
like image 196
Matti Virkkunen Avatar answered Oct 02 '22 15:10

Matti Virkkunen


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

like image 32
Paul Avatar answered Oct 02 '22 16:10

Paul