65.9K
CodeProject is changing. Read more.
Home

PrettyXML (.NET Port)

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jul 12, 2012

CPOL
viewsIcon

8941

This is an alternative for "PrettyXML (.NET Port)"

That sort of utility is best written as a filter so things can be piped in and out:

GenerateSomeXml | MakePretty | SomethingElse
GenerateSomeXml | MakePretty > outfile
type infile | MakePretty > outfile
And you can have it take parameters optionally as well to add flexibility.

As for the basic implementation, I prefer:

System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
doc.LoadXml ( System.Console.In.ReadToEnd() ) ;
System.Xml.XmlWriterSettings xs = new System.Xml.XmlWriterSettings() ;
xs.Indent = true ;
using 
( 
    System.Xml.XmlWriter xw 
= 
    System.Xml.XmlWriter.Create ( System.Console.Out , xs ) 
)
{
    doc.WriteTo ( xw ) ;
}
An important note on XmlWriter is that you must remember to use using or Flush.