Solved

xml winforms viewer

Posted on 2013-05-17
5
730 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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