2010-06-17 12 views

risposta

23

Tutte le API XML standard di .Net formatteranno il loro output.

utilizzando LINQ to XML:

string formatted = XDocument.Parse(source).ToString(); 

O

string formatted = XDocument.Load(path).ToString(); 
+0

Ha funzionato come un fascino. Grazie molto. –

4

Utilizzare i XmlWriterSettings con XmlWriter

var doc = new XmlDocument(); 
doc.Load(@"c:\temp\asdf.xml"); 
var writerSettings = new XmlWriterSettings 
{ 
    Indent = true, 
    NewLineOnAttributes = true, 
}; 

var writer = XmlWriter.Create(@"c:\temp\asdf_pretty.xml", writerSettings); 
doc.Save(writer); 
0

È possibile utilizzare XMLBuilder per generare il codice XML e quindi chiamare il metodo ToString per ottenere un output rientrato.