简体   繁体   English

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

[英]Is there a quick way to format an XmlDocument for display in C#?

I want to output my InnerXml property for display in a web page. 我想输出我的InnerXml属性以在网页中显示。 I would like to see indentation of the various tags. 我想看看各种标签的缩进。 Is there an easy way to do this? 是否有捷径可寻?

Here's a little class that I put together some time ago to do exactly this. 这是我前一段时间拼凑起来做的一个小课程。

It assumes that you're working with the XML in string format. 它假定您使用字符串格式的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();
    }
}

You should be able to do this with code formatters. 您应该可以使用代码格式化程序执行此操作。 You would have to html encode the xml into the page first. 您必须首先将xml编码到页面中。

Google has a nice prettifyer that is capable of visualizing XML as well as several programming languages. 谷歌有一个很好的预处理器 ,能够可视化XML以及几种编程语言。

Basically, put your XML into a pre tag like this: 基本上,将您的XML放入这样的预标签中:

<pre class="prettyprint"> 
    &lt;link href="prettify.css" type="text/css" rel="stylesheet" /&gt;
    &lt;script type="text/javascript" src="prettify.js">&lt;/script&gt;
</pre>

Use the XML Web Server Control to display the content of an xml document on a web page. 使用XML Web服务器控件网页上显示xml文档的内容。

EDIT: You should pass the entire XmlDocument to the Document property of the XML Web Server Control to display it. 编辑:您应该将整个XmlDocument传递给XML Web服务器控件的Document属性以显示它。 You don't need to use the InnerXml property. 您不需要使用InnerXml属性。

If identation is your only cocern and if you can afford to launch xternall process, you can process xml file with HTML Tidy console tool (~100K). 如果identation是您唯一的cocern,并且您可以负担得起启动xternall进程,则可以使用HTML Tidy控制台工具(~100K)处理xml文件。

The code is: 代码是:

tidy --input-xml y --output-xhtml y --indent "1" $(FilePath)

Then you can display idented string on web page once you get rid of special chars. 然后,一旦摆脱了特殊的字符,就可以在网页上显示标题字符串。

It would be also easy to create recursive function that makes such output - simply iterate nodes starting from the root and enter next recursion step for child node, passing identation as a parameter to each new recursion call. 创建递归函数也很容易做出这样的输出 - 简单地从根开始迭代节点并进入子节点的下一个递归步骤,将identation作为参数传递给每个新的递归调用。

Check out the free Actipro CodeHighlighter for ASP.NET - it can neatly display XML and other formats. 查看免费的Actipro CodeHighlighter for ASP.NET - 它可以整齐地显示XML和其他格式。

Or are you more interested in actually formatting your XML? 或者您对实际格式化XML更感兴趣? Then have a look at the XmlTextWriter - you can specify things like Format (indenting or not) and the indent level, and then write out your XML to eg a MemoryStream and read it back from there into a string for display. 然后看一下XmlTextWriter - 你可以指定像Format(缩进或不缩进)和缩进级别之类的东西,然后将你的XML写出来,例如一个MemoryStream,然后从那里读回一个字符串进行显示。

Marc

Use an XmlTextWriter with the XmlWriterSettings set up so that indentation is enabled. 使用XmlTextWriter并设置XmlWriterSettings以便启用缩进。 You can use a StringWriter as "temporary storage" if you want to write the resulting string onto screen. 如果要将结果字符串写入屏幕,可以将StringWriter用作“临时存储”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM