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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

631 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