xml winforms viewer

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

Question by:jackjohnson44
  • 3
  • 2
LVL 42

Assisted Solution

sedgwick earned 500 total points
ID: 39176723

Accepted Solution

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)
            XmlDocument doc = new XmlDocument();
            catch (Exception err)

            ConvertXmlNodeToTreeNode(doc, treeXml.Nodes);

        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 + "?>";
                case XmlNodeType.Element:
                    newTreeNode.Text = "<" + xmlNode.Name + ">";
                case XmlNodeType.Attribute:
                    newTreeNode.Text = "ATTRIBUTE: " + xmlNode.Name;
                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                    newTreeNode.Text = xmlNode.Value;
                case XmlNodeType.Comment:
                    newTreeNode.Text = "<!--" + xmlNode.Value + "-->";

            if (xmlNode.Attributes != null)
                foreach (XmlAttribute attribute in xmlNode.Attributes)
                    ConvertXmlNodeToTreeNode(attribute, newTreeNode.Nodes);
            foreach (XmlNode childNode in xmlNode.ChildNodes)
                ConvertXmlNodeToTreeNode(childNode, newTreeNode.Nodes);

LVL 42

Expert Comment

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

Author Comment

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!!

Author Closing Comment

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.

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now