在Winforms控件中显示XML数据

时间:2020-03-05 18:45:53  来源:igfitidea点击:

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

错误数据包含发生错误时的所有服务器变量。这些已格式化为XML文档,看起来具有以下效果:

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

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

有什么建议吗,或者我正在寻找和自定义实现?

[编辑]需要显示的数据部分:

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

解决方案

回答

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

回答

我们可以使用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类应用此转换。
它应该给出这样的输出:

Server variables:

          ALL_HTTP:HTTP_CONNECTION:close HTTP_USER_AGENT:Mozilla/4.0 (compatible MSIE 6.0; Windows NT 5.1; SV1)

            AUTH_TYPE:

            HTTPS:off

            HTTPS_KEYSIZE:

            HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;S )  
  
  Query string items:

          tid:196

回答

我们可以使用树视图控件,并使用递归XLinq算法将数据放入其中。我自己做了一个界面,允许用户建立自定义XML表示,并且效果很好。

回答

请参阅XML数据绑定。
使用Visual Studio或者xsd.exe从XSD生成DataSet或者类,然后根据需要使用System.Xml.Serialization.XmlSerializer将XML转换为Objects / DataSet。按摩物体。在网格中显示它们。