【问题标题】:Displaying XML data in a Winforms control在 Winforms 控件中显示 XML 数据
【发布时间】:2010-09-07 10:42:02
【问题描述】:

我想在 winforms 应用程序中向用户显示 xml 错误日志的详细信息,并且正在寻找最佳控制来完成这项工作。

错误数据包含发生错误时的所有服务器变量。这些已被格式化为 XML 文档,看起来类似于:

<error>
    <serverVariables>
        <item>
            <value>
        </item>
    </serverVariables>
    <queryString>
        <item name="">
            <value string=""> 
        </item>
    </queryString>      
</error>

我想从存储它的字符串中读取这些数据,并以一种有用的方式通过 Windows 窗体将其显示给用户。 XML 记事本在格式化 xml 方面做得很好,但并不是我真正想要的,因为我宁愿以 Name : string 格式显示项目详细信息。

任何建议或我正在寻找和自定义实现?

[EDIT] 需要显示的一段数据:

<?xml version="1.0" encoding="utf-8"?>
<error host="WIN12" type="System.Web.HttpException" message="The file '' does not exist." source="System.Web" detail="System.Web.HttpException: The file '' does not exist. at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath) at" time="2008-09-01T07:13:08.9171250+02:00" statusCode="404">
  <serverVariables>
    <item name="ALL_HTTP">
      <value string="HTTP_CONNECTION:close HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) " />
    </item>
    <item name="AUTH_TYPE">
      <value string="" />
    </item>
    <item name="HTTPS">
      <value string="off" />
    </item>
    <item name="HTTPS_KEYSIZE">
      <value string="" />
    </item>
    <item name="HTTP_USER_AGENT">
      <value string="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" />
    </item>
  </serverVariables>
  <queryString>
    <item name="tid">
      <value string="196" />
    </item>
  </queryString>
</error>

【问题讨论】:

    标签: c# xml winforms formatting


    【解决方案1】:

    您可以使用XSLT转换您的 XML 数据
    另一种选择是使用 Xlinq。
    如果您想要具体的代码示例,请向我们提供示例数据

    编辑: 这是您的 XML 文件的示例 XSLT 转换:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text"/>
        <xsl:template match="//error/serverVariables">
          <xsl:text>Server variables:
          </xsl:text>
          <xsl:for-each select="item">
            <xsl:value-of select="@name"/>:<xsl:value-of select="value/@string"/>
            <xsl:text>
            </xsl:text>
          </xsl:for-each>
        </xsl:template>
        <xsl:template match="//error/queryString">
          <xsl:text>Query string items:
          </xsl:text>
          <xsl:for-each select="item">
            <xsl:value-of select="@name"/>:<xsl:value-of select="value/@string"/>
            <xsl:text>
            </xsl:text>
          </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    

    您可以使用XslCompiledTransform 类应用此转换。 它应该给出这样的输出:

    服务器变量:
    ALL_HTTP:HTTP_CONNECTION:close HTTP_USER_AGENT:Mozilla/4.0(兼容 MSIE 6.0;Windows NT 5.1;SV1)
    AUTH_TYPE:
    HTTPS:关闭
    HTTPS_KEYSIZE:
    HTTP_USER_AGENT:Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.1;S)

    查询字符串项:
    时间:196

    【讨论】:

      【解决方案2】:

      您可以使用树视图控件并使用递归 Xlinq 算法将数据放入其中。我自己通过一个界面完成了这项工作,该界面允许用户构建自定义 XML 表示,并且效果非常好。

      【讨论】:

        【解决方案3】:

        XML data binding。 使用 Visual Studio 或 xsd.exe 从 XSD 生成数据集或类,然后在需要时使用 System.Xml.Serialization.XmlSerializer 将您的 XML 转换为对象/数据集。按摩物体。在网格中显示它们。

        【讨论】:

          【解决方案4】:

          您可以尝试使用DataGridView 控件。要查看示例,请在 DevStudio 中加载一个 XML 文件,然后右键单击 XML 并选择“查看数据网格”。您需要阅读该控件的 API 文档才能使用它。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-09-07
            • 1970-01-01
            相关资源
            最近更新 更多