tags:

views:

1642

answers:

4

I have a string containing XML document using LinqtoXML

What is the best way of displaying it on an asp.net page as is.

A: 

If you have the XML document in a string variable, you can run it through the HtmlEncode method in order to have it encoded so that you can display the XML data in a web page:

string xmlData = GetXmlData();
Response.Write(Server.HtmlEncode(xmlData));

As Dane mentions, you will want to enclose this output in a PRE tag to preserve the formatting.

Fredrik Mörk
I would *love* to see the motivation for that downvote :)
Fredrik Mörk
A: 

HTML PRE tags in a div, and just echo out the string properly escaped?

Div gives you alignment / formatting, while the PRE should preserve whitespace

Dane
Any Example.i have an XElement xele with me?
Musa
<div id="xmlInner"><pre><asp:label id="xmllabel" runat="server"></pre></div>In your code behind : protected void Page_Load(Object Source, EventArgs E) {string xmlData = GetXmlData();xmlLabel.Text = Server.HtmlEncode(xmlData);}Add CSS styles to the Div and you're away (if you want to)
Dane
+2  A: 

Use the asp:Xml control together with this Default Stylesheet as the TransformSource. This will give you a look very similar to what you see when you open an xml file in Internet Explorer complete with syntax highlighting and coloration.

Edit: Fixed the link! It's now pointing to the version of the file that I use myself. I removed the expanding/collapsing node functionality because I couldn't get that to work. This file is xsl 1.0 and works great without bugs for my purposes.

Dennis Palmer
The link doesn't work
Rune Grimstad
That is how I would do it too... except that defaultss.xsl is not available publicly (and is not XSL 1.0 compliant). However, a public converted version was posted here: dpawson.co.uk/xsl/sect2/…. I have tested it and it works, though a bit buggy. +1
Cerebrus
Fixed the link to point to the version of defaultss.xsl that I use.
Dennis Palmer
still broken link :(
sebastian
@sebastian I recently changed web hosts and forgot to move that file over. I'll find the file again and upload it to that same location on my new host.
Dennis Palmer
the link is still broken
Michiel Overeem
@Michiel my web host changed yet again. The link should work now.
Dennis Palmer
+2  A: 

I would have liked to do it the way Dennis has mentioned (using an <asp:Xml> control). But that necessitates the use of an XSL stylesheet to format the XML. The Xml control does not allow an HTML encoded string to be passed as the DocumentContent property and does not expose any method to encode the content.

As I have mentioned in the comments to Dennis' post, the defaultss.xsl contained within msxml.dll is not available publicly (and is not XSL 1.0 compliant). However, a public converted version was posted here: http://www.dpawson.co.uk/xsl/sect2/microsoft.html#d7615e227. I have tested it and it works, though a bit buggy.

Therefore, I think the simplest way would be to use an <asp:Literal> control to output pre-encoded XML to the page. The following sample demonstrates this method:


<%@ Page Language="C#" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    string theXML = Server.HtmlEncode(File.ReadAllText(Server.MapPath("~/XML/myxmlfile.xml")));
    lit1.Text = theXML;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <pre>
        <asp:Literal ID="lit1" runat="server" />
      </pre>
    </div>
  </form>
</body>
</html>
Cerebrus
Will this also render the line feeds?
Andrew Shepherd