[C#]格式化XmlDocument

 1string s = “<Test><a></a></Test>”;
 2System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
 3doc.LoadXml(s);
 4System.IO.MemoryStream ms = new System.IO.MemoryStream();
 5System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
 6xmlWriter.Indentation = 4;
 7xmlWriter.Formatting = System.Xml.Formatting.Indented;
 8doc.WriteContentTo(xmlWriter);
 9xmlWriter.Close();
10string result = Encoding.UTF8.GetString(ms.ToArray());
posted on 2008-06-25 11:24  JIN Weijie  阅读(1215)  评论(0编辑  收藏  举报