开发者

How to Display Formatted XML

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.


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();
    }
}


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


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


Declare yourself an asp:Literal within a <pre> element.

<pre><asp:Literal ID="foo" runat="server" /></pre>

Assign the xml string to the literal:

foo.Text = CreatedIndentedXmlString("<foo><bar>6</bar></foo>");

public string CreatedIndentedXmlString(string inXml)
{
    if (inXml.Length == 0) return "";
    XElement x = XElement.Parse(inXml);
    return Server.HtmlEncode(x.ToString());
}


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">
<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"/>

<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>


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?


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜