Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting of XML created by DataContractSerializer

Is there an easy way to get DataContractSerializer to spit out formatted XML rather then one long string? I don't want to change the tags or content in any way, just have it add line breaks and indentation to make the XML more readable?

<tagA>    <tagB>This is</tagB>       <tagC>Much</tagC>    <tagD>       <tagE>easier to read</tagE>    </tagD> </tagA>   <tagA><tagB>This is</tagB><tagC>Much</tagC><tagD><tagE>harder to read</tagE></tagD></tagA> 
like image 831
Eric Anastas Avatar asked Apr 10 '09 22:04

Eric Anastas


1 Answers

As bendewey says, XmlWriterSettings is what you need - e.g. something like

var ds = new DataContractSerializer(typeof(Foo));  var settings = new XmlWriterSettings { Indent = true };  using (var w = XmlWriter.Create("fooOutput.xml", settings))     ds.WriteObject(w, someFoos); 
like image 62
Steve Willcock Avatar answered Sep 20 '22 17:09

Steve Willcock