// 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());
本文转自Jack Niu博客园博客,原文链接:http://www.cnblogs.com/skywind/archive/2009/05/09/1453094.html,如需转载请自行联系原作者