Solved

how to read xml and populate listview

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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