首页 / 知识

关于c#:在Winforms控件中显示XML数据

2023-04-13 19:30:00

关于c#:在Winforms控件中显示XML数据

Displaying XML data in a Winforms control

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

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

1
2
3
4
5
6
7
8
9
10
11
12
<error>
    <serverVariables>
        <item>
            <value>
        </item>
    </serverVariables>
    <queryString>
        <item name="">
            <value string="">
        </item>
    </queryString>      
</error>

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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>

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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


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


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


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


控件显示数据用户

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢