Created
August 28, 2015 15:34
-
-
Save decriptor/b8c40a60ace3a24d7604 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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