[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());
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());