Indent Xml - CSharp System.Xml

CSharp examples for System.Xml:XML Format

Description

Indent Xml

Demo Code


using System.Xml;
using System.IO;//  w w  w  .j a va 2  s  .  c  o m

public class Main{
        internal static string IndentXml(string unformatedXml)
        {
            MemoryStream ms = new MemoryStream();
            XmlTextWriter xtw = new XmlTextWriter(ms, System.Text.Encoding.Unicode);
            XmlDocument doc = new XmlDocument();

            try
            {
                doc.LoadXml(unformatedXml);

                xtw.Formatting = Formatting.Indented;
                doc.WriteContentTo(xtw);
                xtw.Flush();

                ms.Seek(0, SeekOrigin.Begin);
                StreamReader sr = new StreamReader(ms);
                return sr.ReadToEnd();
            }
            catch
            {
                return "Error while formatting Xml...";
            }
        }
}

Related Tutorials