Skip to content

Instantly share code, notes, and snippets.

@decriptor
Created August 28, 2015 15:34
Show Gist options
  • Save decriptor/b8c40a60ace3a24d7604 to your computer and use it in GitHub Desktop.
Save decriptor/b8c40a60ace3a24d7604 to your computer and use it in GitHub Desktop.
using System;
using System.Text;
using System.Xml;
using System.IO;
namespace TestXmlFormatter
{
class MainClass
{
public static void Main (string[] args)
{
string xml = "<response><error code='1'> Success</error></response>";
Console.WriteLine (xml);
var formattedXml = Beautify (xml);
Console.WriteLine (formattedXml);
var x = Console.ReadLine ();
}
static public string Beautify(string xml)
{
string result;
var encoding = new UTF8Encoding (false);
using (MemoryStream output = new MemoryStream ()) {
var reader = XmlReader.Create (new StringReader (xml));
using (XmlWriter writer = XmlWriter.Create (output, new XmlWriterSettings { Indent = true, Encoding = encoding })) {
writer.WriteNode (reader, true);
}
result = Encoding.UTF8.GetString (output.ToArray ());
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment