how to filter data from xml file for displaying only selected as nodes in treeview

Posted by michale on Stack Overflow See other posts from Stack Overflow or by michale
Published on 2012-03-26T10:52:52Z Indexed on 2012/03/26 11:29 UTC
Read the original article Hit count: 251

Filed under:
|
|
|

I have an xml file named "books.xml" provided in the link "http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx". What my requirement was to disaplay only the <title> from xml information as nodes in tree view. But when i did the following coding its displaying all the values as nodes like "catalog" as rootnode, book as parent node for all then author,title,genre etc as nodes but i want only root node catalogue and title as nodes not even book. Can any body guide me what modification i need to do in the exisitng logic for displaying title as nodes

OpenFileDialog dlg = new OpenFileDialog();
        dlg.Title = "Open XML document";
        dlg.Filter = "XML Files (*.xml)|*.xml";
        dlg.FileName = Application.StartupPath + "\\..\\..\\Sample.xml";
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            try
            {
                //Just a good practice -- change the cursor to a 
                //wait cursor while the nodes populate
                this.Cursor = Cursors.WaitCursor;
                //First, we'll load the Xml document
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(dlg.FileName);
                //Now, clear out the treeview, 
                //and add the first (root) node
                treeView1.Nodes.Clear();
                treeView1.Nodes.Add(new
                  TreeNode(xDoc.DocumentElement.Name));
                TreeNode tNode = new TreeNode();
                tNode = (TreeNode)treeView1.Nodes[0];
                //We make a call to addTreeNode, 
                //where we'll add all of our nodes
                addTreeNode(xDoc.DocumentElement, tNode);
                //Expand the treeview to show all nodes
                treeView1.ExpandAll();
            }
            catch (XmlException xExc)
            //Exception is thrown is there is an error in the Xml
            {
                MessageBox.Show(xExc.Message);
            }
            catch (Exception ex) //General exception
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default; //Change the cursor back
            }
        }}
        //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();
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml