Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

PrettyXML (.NET Port)

5.00/5 (1 vote)
12 Jul 2012CPOL 8.9K  
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)