Tuesday, June 5, 2007

Asp.Net & Formatting XML string – code snippet

A simple code snippet to show how to format XML string with proper indentation:


public static string FormatXml(string unformattedXML)
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(unformattedXML);
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (XmlTextWriter xtw = new XmlTextWriter(sw))
{
xtw.Formatting = Formatting.Indented;
xd.WriteTo(xtw);
}
}
return sb.ToString();
}

Love coding!

No comments: