[C#] Format a XML string using XmlTextWriter

05/09/2009
 // a demo string
 string xml = "<Root><Eles><Ele>abc</Ele><Ele>123</Ele></Eles></Root>";

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xml);

System.IO.StringWriter sw = new System.IO.StringWriter();
using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(sw))
{
writer.Indentation = 2;// the Indentation
writer.Formatting = System.Xml.Formatting.Indented;
doc.WriteContentTo(writer);
writer.Close();
}

// out put the formated xml
 Console.WriteLine(sw.ToString());
Posted in: Software Programming C# and .NET| Tags: NET Programming C# XmlTestWriter XML Format

Hot Posts

Latest posts

Tags

Others

Sponsors

asp.net interview questions