views:

226

answers:

7

I have xml data that I am returning to my view. I am putting it in a textarea, but this displays it unformatted. How can I format the xml for display in my view?

I will be displaying the xml in only part of the page, so I can't let IE display it. I want it to be in standard xml indented format.

Thanks

+3  A: 

You can use an XSLT to convert your XML into XHTML and then display that.

Jeff Yates
+1  A: 

how about replacing "<" with &lt; and then stuffing in a <pre> block. User won't be able to edit it but surely you have better tools than a textarea for that anyway, right?

No Refunds No Returns
A: 

You can use java script code colorizer. as for formatting, xml writer (as I know) can output well structured document, it can be turned off by default to reduce weight.

Sergey Mirvoda
+1  A: 

If, by "formatting", you mean indented then you can load it into XmlDocument and write it into an XmlWriter initialized with an XmlWriterSettings that has Indent set to true.

private string FormatXml(string input)
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(input);

    using (StringWriter buffer = new StringWriter())
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using (XmlWriter writer = XmlWriter.Create(buffer, settings))
        {
            doc.WriteTo( writer );

            writer.Flush();
        }

        buffer.Flush();

        return buffer.ToString();
    }
}
Richard Szalay
-1: `XmlTextWriter` is deprecated as of .NET 2.0. You're also missing `using` blocks.
John Saunders
`XmlTextWriter` is most certainly not deprecated as of .NET 4.0. It's simply *recommended* that `XmlWriter.Create` is used instead of the XmlTextWriter ctor. I've updated the code to dispose of the resources, though it was only a sample to begin with.
Richard Szalay
@Richard: if you use "@user", it informs the user of the comment. I've removed the downvote. "only a sample" code gets copied and pasted. I've also frequently seen people asked, "why did you write it that way", answer, "because of the sample".
John Saunders
A: 

If you are looking for a simple Xml structure the default IE view (with the collapsible nodes etc).

The IE transform is here: res://msxml.dll/DEFAULTSS.xsl.

An xslt version is available online at places like this. I use this in places where I don't have a more human friednly transform but still want to see the Xml in a formatted manner.

dkackman
+2  A: 

Simply load the XML into an XElement, then use XElement.ToString().

John Saunders
+1 for simplicity
Joe
A: 

Here's what I like to do:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="xml" version="1.0" standalone="yes" omit-xml-declaration="yes" 
    encoding="utf-8" media-type="text/html" indent="no" cdata-section-elements="" 
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/&gt;

<xsl:template match="/">
    <html><head><title>XML Data</title>
    <style type="text/css">th {text-align:right}</style></head>
    <body><table><xsl:apply-templates/></table></body></html>
</xsl:template>

<xsl:template match="*[text() and *]">
    <xsl:apply-templates select="@*"/>
    <tr><th><xsl:value-of select="name()"/></th>
        <td>
            <xsl:for-each select="*|text()">
                <xsl:choose>
                    <xsl:when test="name()">
                        <xsl:apply-templates mode="inline" select="."/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </td>
    </tr>
</xsl:template>

<xsl:template match="*" mode="inline">
    <xsl:text> [ </xsl:text>
    <strong><xsl:value-of select="name()"/></strong>
    <xsl:if test="@*">
        <xsl:text> ( </xsl:text>
        <xsl:for-each select="@*"><xsl:if test="position() &gt; 1" xml:space="preserve"> </xsl:if>
            <em><xsl:value-of select="name()"/></em> = <xsl:value-of select="."/>
        </xsl:for-each>
        <xsl:text> )</xsl:text>
    </xsl:if>
    <xsl:text>: </xsl:text>
    <xsl:apply-templates mode="inline"/>
    <xsl:text> ] </xsl:text>
</xsl:template>

<xsl:template match="*[text() and not(*)]">
    <xsl:apply-templates select="@*"/>
    <tr><th><xsl:value-of select="name()"/></th>
        <td><xsl:apply-templates/></td></tr> 
</xsl:template>

<xsl:template match="*[(* or @*) and not(text())]">
    <tr><td colspan="2"><fieldset><legend><xsl:value-of select="name()"/></legend>
        <table><xsl:apply-templates select="@*"/><xsl:apply-templates/></table></fieldset></td></tr>
</xsl:template>

<xsl:template match="*[not(*|@*|text())]">
    <tr><td colspan="2"><xsl:value-of select="name()"/></td></tr>
</xsl:template>

<xsl:template match="@*">
    <tr><th><em><xsl:value-of select="name()"/></em></th>
        <td><xsl:value-of select="."/></td></tr>
</xsl:template>

</xsl:stylesheet>
brianary