如何以XML方式在Winform中显示Xml数据? [英] How to show Xml data in the winform in the XML fashion?

查看:41
本文介绍了如何以XML方式在Winform中显示Xml数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的winform应用程序与Web服务通信.对于某些需求,我需要在winform应用程序中以XML格式显示Web服务响应.

My winform application communicates with the web service. For some requirement, I need to present the web service response in the XML format in the winform application.

我可以将响应类型(类)实例序列化为XML字符串.但是,当我在富文本框中显示该字符串时,显然会显示为连续字符串;而不是如下所示的XML.

I can serialize the response type (class) instance to XML string. But when i show that string in the rich text box, it's obviously displayed as continuous string; not as the XML as shown below.

<UserServiceAccesses>
- <UserServiceAccess>
-   <Service>
       <ID>0</ID> 
       <LocalID>Loggerr</LocalID> 
       <SystemID>-1</SystemID> 
       <ServiceType>U</ServiceType> 
       <Name>MyLogger</Name> 
       </Service>
    <ClientPermissions /> 
  </UserServiceAccess>
- <UserServiceAccess>
-     <Service>
         <ID>0</ID> 
         <LocalID>Logger2</LocalID> 
         <SystemID>-1</SystemID> 
         <ServiceType>U</ServiceType> 
         <Name>MyLogger2</Name> 
     </Service>
     <ClientPermissions /> 
  </UserServiceAccess>
<UserServiceAccesses>

在此, UserServiceAccesses 是具有 UserServiceAccess 类型的属性的类.然后 UserServiceAccess 具有类型为 Service ClientPermissions

Here, UserServiceAccesses is the class which has property of type UserServiceAccess. Then UserServiceAccess has a properties of type Service, ClientPermissions

我该怎么办?它可以采用任何形式(树,表,文本等),但应作为XML可读.由于我们从应用程序中调用了许多Web方法,因此每次XML结构都会有所不同,因此我们无法拥有明确的架构.

How can I do it? It can be in any form (tree, table, text etc.) but it should be readable as XML. Since there are many web methods we call from application, everytime XML structure would be different and so we cannot have definite schema.

推荐答案

使用 TreeView控件

这是在 treeview 上显示xml的有效代码:

Here is working code to display xml on treeview :

using System;
using System.Windows.Forms;
using System.Xml;

public class XmlTreeDisplay : System.Windows.Forms.Form
{
    private System.Windows.Forms.TreeView treeXml = new TreeView();

    public XmlTreeDisplay()
    {
        treeXml.Nodes.Clear();
        this.Controls.Add(treeXml);
        // Load the XML Document
        XmlDocument doc = new XmlDocument();
        try
        {
            doc.LoadXml("<books><A property='a'><B>text</B><C>textg</C><D>99999</D></A></books>");
            //doc.Load("");
        }
        catch (Exception err)
        {

            MessageBox.Show(err.Message);
            return;
        }

        ConvertXmlNodeToTreeNode(doc, treeXml.Nodes);
        treeXml.Nodes[0].ExpandAll();
    }

    private void ConvertXmlNodeToTreeNode(XmlNode xmlNode,
      TreeNodeCollection treeNodes)
    {

        TreeNode newTreeNode = treeNodes.Add(xmlNode.Name);

        switch (xmlNode.NodeType)
        {
            case XmlNodeType.ProcessingInstruction:
            case XmlNodeType.XmlDeclaration:
                newTreeNode.Text = "<?" + xmlNode.Name + " " +
                  xmlNode.Value + "?>";
                break;
            case XmlNodeType.Element:
                newTreeNode.Text = "<" + xmlNode.Name + ">";
                break;
            case XmlNodeType.Attribute:
                newTreeNode.Text = "ATTRIBUTE: " + xmlNode.Name;
                break;
            case XmlNodeType.Text:
            case XmlNodeType.CDATA:
                newTreeNode.Text = xmlNode.Value;
                break;
            case XmlNodeType.Comment:
                newTreeNode.Text = "<!--" + xmlNode.Value + "-->";
                break;
        }

        if (xmlNode.Attributes != null)
        {
            foreach (XmlAttribute attribute in xmlNode.Attributes)
            {
                ConvertXmlNodeToTreeNode(attribute, newTreeNode.Nodes);
            }
        }
        foreach (XmlNode childNode in xmlNode.ChildNodes)
        {
            ConvertXmlNodeToTreeNode(childNode, newTreeNode.Nodes);
        }
    }
    public static void Main()
    {
        Application.Run(new XmlTreeDisplay());
    }
}

这篇关于如何以XML方式在Winform中显示Xml数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆