开发者

Is there an XmlWriter that writes a colorful HTML-formatted output for displaying XML in a webpage?

开发者 https://www.devze.com 2022-12-30 07:54 出处:网络
I\'ve got a bi开发者_JAVA百科t of XML I want to display on my ASP.NET website as-is (for debugging purposes), and it would be nice if it was colored. This should be easy to achieve with the right kind

I've got a bi开发者_JAVA百科t of XML I want to display on my ASP.NET website as-is (for debugging purposes), and it would be nice if it was colored. This should be easy to achieve with the right kind of XmlWriter, but I don't have the time to make one myself. Is there an existing (free) component that can do this?


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:

Is there an XmlWriter that writes a colorful HTML-formatted output for displaying XML in a webpage?

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.


That's going to be more a function of your editor than of the XML file itself, so no, XmlWriter won't do that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号