Solved

xml winforms viewer

Posted on 2013-05-17
5
751 Views
Last Modified: 2013-05-22
I have a win form c# application that uses web services.  I am outputting the xml into a text area, but it isn't too readable.  Does anyone know of some control that I can use to view the xml better?  I can open it in internet explorer if i save it to a file, but I am not sure in C#.  I found a post, but the link for files was down.  This isn't huge for me, so I don't want to spend a ton of time.  i was hoping for something easy and pre-built.

Thanks!
0
Comment
Question by:jackjohnson44
  • 3
  • 2
5 Comments
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 500 total points
ID: 39176723
0
 

Accepted Solution

by:
jackjohnson44 earned 0 total points
ID: 39176771
I used the second link, then the second answer on that page.  Basically it creates a tree view control and sets its source to an xml string.  It was a little difficult because it was a console app that just opened.

Its easy to use.  You just need two functions.  In this example "treeXml" is a TreeView added to the page.

just call: SetTreeViewSourceToXml(xmlSource);

        private void SetTreeViewSourceToXml(string xmlSource)
        {
            treeXml.Nodes.Clear();
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(xmlSource);
            }
            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);
            }
        }

    }
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39176772
so did u manage to run it?
do u need any further help?
0
 

Author Comment

by:jackjohnson44
ID: 39176791
I'm good thanks.  That works great.  My code above will work.   I only marked that as the answer so people would see it.  I gave you the points.

Thanks and have a good weekend!!
0
 

Author Closing Comment

by:jackjohnson44
ID: 39186901
Thanks a lot! I want people to read my answer because it will save a lot of time.  The link was helpful but was geared toward a console app.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question