Writing formatted XML with XmlWriter Writing formatted XML with XmlWriter xml xml

Writing formatted XML with XmlWriter


I suspect you need to create an XmlWriterSettings with the behaviour you want (indentation etc) and then pass that to the XmlWriter on creation. Just setting Indent to true may well be enough:

XmlWriterSettings settings = new XmlWriterSettings { Indent = true };using (XmlWriter writer = XmlWriter.Create(..., settings)){    ...}


You can customize the xml output via the XmlWriterSettings.

You didn't include any code, but you can set the XmlWriterSettings when you create the XmlWriter. You can also just use something like:

var myXmlWriter = new XmlWriterSettings { Indent = true };


If, like me, you're implementing your own XmlWriter you can do:

var myXmlWriter = new MyXmlWriter(stream, System.Text.Encoding.UTF8){    Formatting = Formatting.Indented};

or do this.Formatting = Formatting.Indented in it's constructor.