c# - 有没有一种快速的方法来格式化 XmlDocument 以便在 C# 中显示?

标签 c# asp.net xml

我想输出我的 InnerXml 属性以在网页中显示。我想查看各种标签的缩进。有没有简单的方法可以做到这一点?

最佳答案

这是我前段时间整理的一个小类(class),就是为了做到这一点。

它假定您正在使用字符串格式的 XML。

public static class FormatXML
{
    public static string FormatXMLString(string sUnformattedXML)
    {
        XmlDocument xd = new XmlDocument();
        xd.LoadXml(sUnformattedXML);
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        XmlTextWriter xtw = null;
        try
        {
            xtw = new XmlTextWriter(sw);
            xtw.Formatting = Formatting.Indented;
            xd.WriteTo(xtw);
        }
        finally
        {
            if(xtw!=null)
                xtw.Close();
        }
        return sb.ToString();
    }
}

关于c# - 有没有一种快速的方法来格式化 XmlDocument 以便在 C# 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/771652/

相关文章:

c# - VS2010 上的 TFS,合并代码问题

c# - 将文件写入任何计算机上的项目文件夹

c# - 在 C# 中使用静态方法实现接口(interface)

c# - 不实现接口(interface)成员

java - 如何在 android 中设置单个 listView 项目的样式?

c# - 替代 else if 对于随机数 C#.net

c# - 为什么asp :DropDownList does not respond to code-behind

c# - 在指定时间运行报告

xml - emacs xpath xml xslt

php - Wordpress Yoast 从 XML 站点地图中排除帖子