如何从xml文件中筛选数据,以便在树视图中仅显示选定的节点

本文关键字:显示 视图 节点 xml 文件 筛选 数据 | 更新日期: 2023-09-27 18:28:58

链接中提供了一个名为"books.xml"的xml文件http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx"。我的要求是在树视图中只将<title>从xml信息中作为节点来显示。但当我进行以下编码时,它将所有值显示为节点,如"catalog"作为根节点,书籍作为所有的父节点,然后作者、标题、流派等作为节点,但我只想要根节点目录和标题作为节点,甚至不想要书籍。任何人都能指导我在现有逻辑中需要做什么修改,以将标题显示为节点吗?"

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();
    }

如何从xml文件中筛选数据,以便在树视图中仅显示选定的节点

我认为您的意图只是在类别节点下显示标题,而不显示其他内容。在这种情况下,请尝试以下版本的addTreeNode方法:

    private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
    {
        XmlNode xNode;
        TreeNode tNode;
        XmlNodeList xNodeList;
        if (xmlNode.HasChildNodes && xmlNode.Name != "title") //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, treeNode);
            }
        }
        else if (xmlNode.Name == "title") //No children, so add the outer xml (trimming off whitespace)
            treeNode.Nodes.Add(new TreeNode(xmlNode.InnerText));
    }

然而,我必须强调,这是实现目标的非常低效和不雅的方式。实际上,只需使用如下XPath表达式就可以做到这一点:

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();
            TreeNode rootTreeNode = new TreeNode(xDoc.DocumentElement.Name);
            treeView1.Nodes.Add(rootTreeNode);
            foreach (XmlNode titleNode in xDoc.DocumentElement.SelectNodes(@"//title"))
            {
                rootTreeNode.Nodes.Add(titleNode.InnerText);
            }
            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
        }
    }}