格式化xml字符串

/// <summary>
/// xml帮助类
/// </summary>
public class XmlHelper
{

/// <summary>
/// 格式化xml字符串
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static string XmlToFormate(string xml)
{

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;
writer.Formatting = System.Xml.Formatting.Indented;
doc.WriteContentTo(writer);
writer.Close();
}
return sw.ToString();
}
}

posted @ 2022-08-25 14:03  .net&new  阅读(154)  评论(0编辑  收藏  举报