markmnl
markmnl

Reputation: 11326

How to tidy a .Net XmlDocument text before writing it to file?

Using XmlDocument Class: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument(VS.85).aspx

How can I tidy the XML text so it is indented and placed on new lines correctly before calling Save()?

Upvotes: 0

Views: 336

Answers (1)

Sean
Sean

Reputation: 62522

Use the Save() overload that takes an XmlWriter and configure the XmlWriter using a XmlWriterSettings instance. For example

 XmlWriterSettings settings = new XmlWriterSettings();
 settings.Indent = true;
 settings.IndentChars = "  ";
 settings.NewLineOnAttributes = true;

 //Using XmlWriter to create xml file.
 using (XmlWriter writer = XmlWriter.Create("some-file.xml",settings))
 {
  doc.Save(writer)
 }

Upvotes: 1

Related Questions