2010-01-27 110 views
6

我有返回到我的视图的xml数据。我把它放在textarea中,但是这显示它没有格式化。我如何格式化xml以在我的视图中显示?如何显示格式化的XML

我只会在页面的一部分显示xml,所以我不能让IE显示它。我希望它是标准的XML缩进格式。

+4

你怎么想它格式化? – 2010-01-27 17:46:53

+0

我希望缩进标签。 – Joe 2010-01-27 18:30:39

+1

在这种情况下,请参阅我的或John Saunders的答案。 – 2010-01-27 19:26:00

回答

6

只需将XML加载到XElement中,然后使用XElement.ToString()

+0

+1简单 – Joe 2010-01-27 19:41:03

+1

尽管没有缩进,OP要求 – Grubl3r 2014-09-26 09:39:22

4

您可以使用XSLT将您的XML转换为XHTML,然后显示它。

2

如何取代“<”与& lt;然后填入< pre>块。用户将无法编辑它,但无论如何你肯定拥有比textarea更好的工具,对吗?

1

您可以使用java脚本代码着色器。至于格式化,xml writer(据我所知)可以输出结构良好的文档,默认情况下它可以关闭以减轻重量。

5

如果通过“格式化”,你的意思是缩进,那么你可以将其加载到XmlDocument,并将其写入与XmlWriterSettingsIndent设置为true初始化的XmlWriter

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

-1:XmlTextWriter从.NET 2.0开始被弃用。你也缺少'使用'块。 – 2010-01-27 18:09:07

+1

从.NET 4.0开始,绝对不会弃用XmlTextWriter。简单*建议*使用'XmlWriter.Create'来代替XmlTextWriter ctor。我更新了代码来处理资源,尽管它只是一个样本。 – 2010-01-27 19:21:52

+0

如果使用“@user”,它会通知用户注释。我已经删除了downvote。 “只有一个示例”代码被复制并粘贴。我也经常看到有人问,“你为什么这样写”,回答,“因为样本”。 – 2010-02-08 01:43:26

1

如果您正在寻找一个简单的Xml结构默认IE视图(与可折叠节点等)。

IE变换在这里:res://msxml.dll/DEFAULTSS.xsl

xslt版本可在线获得,如this。我在没有更人性化转换的地方使用它,但仍希望以格式化方式查看Xml。

3

这是我喜欢做的事:

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

宣布自己的asp:Literal一个<pre>元素中。

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

指定XML字符串字面:

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