无缩进的XML字符串的处理

加上缩进,输出xml字符串,代码:

using System.Xml;
using System.Text;
 
/// <summary>
/// Format xml string without indent 
/// to xml string with indent 
/// </summary>
/// <param name="source">xml string without indent </param>
/// <returns>xml string with indent </returns>
private string FormatXml(string source)
{
    StringBuilder sb = new StringBuilder();
    XmlTextWriter writer = null;
 
    try
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(source);
 
        writer = new XmlTextWriter(new StringWriter(sb));
        writer.Formatting = Formatting.Indented;
 
        doc.WriteTo(writer);
    }
    finally
    {
        if (writer != null) writer.Close();
    }
 
    return sb.ToString();
}

posted on 2008-05-02 13:20  Mainz  阅读(670)  评论(0编辑  收藏  举报

导航