Solved

how to read xml and populate listview

Posted on 2013-06-18
1
377 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now