Search code examples
c#.netwinformsxml-serialization

How to show Xml data in the winform in the XML fashion?


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.

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>

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

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.


Solution

  • Make use of TreeView control

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