How to Display Formatted XML How to Display Formatted XML xml xml

How to Display Formatted XML


If, by "formatting", you mean indented then you can load it into XmlDocument and write it into an XmlWriter initialized with an XmlWriterSettings that has Indent set to true.

private string FormatXml(string input){    XmlDocument doc = new XmlDocument();    doc.LoadXml(input);    using (StringWriter buffer = new StringWriter())    {        XmlWriterSettings settings = new XmlWriterSettings();        settings.Indent = true;        using (XmlWriter writer = XmlWriter.Create(buffer, settings))        {            doc.WriteTo( writer );            writer.Flush();        }        buffer.Flush();        return buffer.ToString();    }}


Simply load the XML into an XElement, then use XElement.ToString().


You can use an XSLT to convert your XML into XHTML and then display that.