Solved

how to read xml and populate listview

Posted on 2013-06-18
1
376 Views
Last Modified: 2013-06-21
hi experts,  I want to read this xml:

http://www.mercadolivre.com.br/jm/categsXml?as_site_id=MLB&as_max_level=2

and populate an listview with the category, as you can see there is subcategory inside the category, so i want this like an tree-view  but i can't accomplish this, i just don't find the right way to read this xml and process data. I am trying to use xmldatasource but no results. can you guys give me an direction?
0
Comment
Question by:rafaelrgl
1 Comment
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39256151
here how to populate treeview control from your xml url u posted:

        private void populateTreeview()
        {
            WebRequest req = WebRequest.Create("http://www.mercadolivre.com.br/jm/categsXml?as_site_id=MLB&as_max_level=2");
            WebResponse resp = req.GetResponse();
            StreamReader textReader = new StreamReader(resp.GetResponseStream());
            XmlTextReader xmlReader = new XmlTextReader(textReader);

            xmlReader.XmlResolver = null;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.XmlResolver = null;
            xmlDoc.Load(xmlReader);
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(new TreeNode(xmlDoc.DocumentElement.Name));
            TreeNode tNode = new TreeNode();
            tNode = (TreeNode)treeView1.Nodes[0];
            addTreeNode(xmlDoc.DocumentElement, tNode);
            treeView1.ExpandAll();

        }
        //This function is called recursively until all nodes are loaded
        private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
        {
            XmlNode xNode;
            TreeNode tNode;
            XmlNodeList xNodeList;
            if (xmlNode.HasChildNodes) //The current node has children
            {
                xNodeList = xmlNode.ChildNodes;
                for (int x = 0; x <= xNodeList.Count - 1; x++)
                //Loop through the child nodes
                {
                    xNode = xmlNode.ChildNodes[x];
                    treeNode.Nodes.Add(new TreeNode(xNode.Name));
                    tNode = treeNode.Nodes[x];
                    addTreeNode(xNode, tNode);
                }
            }
            else //No children, so add the outer xml (trimming off whitespace)
                treeNode.Text = xmlNode.OuterXml.Trim();
        }

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

16 Experts available now in Live!

Get 1:1 Help Now