2

我有一些 XML 想要按原样显示在我的 ASP.NET 网站上(用于调试目的),如果它是彩色的就好了。这应该很容易通过正确的类型来实现XmlWriter,但我没有时间自己制作。是否有现有的(免费)组件可以做到这一点?

4

2 回答 2

4

I would not use an XmlWriter.

I'd use XSLT. If the XML file is to be displayed by itself, just embed a stylesheet PI into the XML: <?xml-stylesheet type="text/xsl" href="RawXmlAsHtml.xslt"?>. But since IE does this automatically, I'll assume your goal is not to display XML in its own page.

More likely the "raw" xml is to be displayed as part of another HTML page. In that case I'd use an XSL Transform on the server side to produce HTML from XML, then insert the output into a <asp:xml> control. Like this:

var doc= new System.Xml.XmlDocument();
doc.Load(xmlFile);
var xsl= new System.Xml.Xsl.XslTransform();
xsl.Load(Server.MapPath("RawXmlAsHtml.xslt"));
xml1.Document = doc;
xml1.Transform = xsl;

And the markup is:

<asp:xml id="xml1" runat="server" />

That leaves the question of, what XSLT can you use?

IE, since MSXML3, has included a stylesheet to format "raw" xml. It is sometimes accessible via res://msxml3.dll/defaultss.xsl . But this is not a XSLT standard stylesheet; it uses the Microsoft-specific WD-xsl format. It may not be what you want.

I looked and found something that complies to the XSLT standard; produced by Oleg Tkachenko and shipped as part of his eXml web control. It's available under a BSD-style license. (You might even want the entire exml control - I don't know what it is.)

Using that XSLT and the code above, the display looks like this:

alt text

It's not quite perfect, because that stylesheet generates a full HTML page, with <HTML> and <HEAD> tags, etc. You really just want a fragment. But you should be able to tweak it pretty easily, and anyway, it displayed properly for me, unmodified.


Edit: regarding the issue I mentioned: I modified the stylesheet to just not inject the <HTML> and <HEAD> tags. It works great.

于 2010-05-13T14:52:34.977 回答
0

这将更多是您的编辑器的功能,而不是 XML 文件本身的功能,所以不,XmlWriter 不会这样做。

于 2010-05-13T10:06:58.060 回答