XML示例:

   <info>
   <memu>1</memu>
   <datainfo>demo1</datainfo>
   </info>
   <info>
   <memu>2</memu>
   <datainfo>demo2</datainfo>
   </info>
   <info>
   <memu>2</memu>
   <datainfo>demo3</datainfo>
   </info>
。。
需求描述:
  1.查出节点为memu的值设为父节点,datainfo的值设置为子节点。
  2.有重复的memu的值就并和到一个父节点下。  生成如下所示树状列:
   1
     demo1
   2 
     demo2
     demo3问题:我已用递归取到每个info内的节点集,但不知如何判断单个节点是否为memu,用SelectSingleNode报错。

解决方案 »

  1.   

     private void XmlOperation_Load(object sender, EventArgs e)
            {
                path = AppDomain.CurrentDomain.BaseDirectory + @"NameList.xml";
                xml.Load(path);//加载xml文件
                bindTvXml();
            }        /// <summary>
            /// 绑定TreeView
            /// </summary>
            private void bindTvXml()
            {
                for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
                {
                    XmlNode Xnode = xml.DocumentElement.ChildNodes[i];
                    TreeNode node = new TreeNode();
                    node.Text = Xnode.Attributes["name"].Value;
                    node.Tag = Xnode;
                    bindChildNode(node, Xnode);//绑定子节点
                    TvXml.Nodes.Add(node);
                    TvXml.HideSelection = false;
                }
            }
            
            /// <summary>
            /// 递归绑定子节点
            /// </summary>
            /// <param name="node"></param>
            /// <param name="xml"></param>
            private void bindChildNode(TreeNode node, XmlNode xml)
            {
                for (int i = 0; i < xml.ChildNodes.Count; i++)
                {
                    TreeNode Childnode = new TreeNode();
                    XmlNode ChildXml = xml.ChildNodes[i];
                    Childnode.Text = ChildXml.Value;
                    Childnode.Name = "1";
                    Childnode.Tag = xml.ChildNodes[i];
                    if (ChildXml.HasChildNodes)
                    {
                        if (ChildXml.ChildNodes[0].NodeType == XmlNodeType.Text)
                            Childnode.Text = ChildXml.ChildNodes[0].InnerText;
                        else
                            bindChildNode(Childnode, ChildXml);
                    }
                    node.Nodes.Add(Childnode);
                }
               
            }
      

  2.   

      protected void Page_Load(object sender, EventArgs e)
            {
                string xml = @"<info>
                                  <memu>1</memu>
                                  <datainfo>demo1</datainfo>
                                  </info>
                                  <info>
                                  <memu>2</memu>
                                  <datainfo>demo2</datainfo>
                                  </info>
                                  <info>
                                  <memu>2</memu>
                                  <datainfo>demo3</datainfo>
                                  </info>";
                XElement x = XElement.Load(Server.MapPath("~/Web.config"));
                foreach (XElement r in x.Elements())
                {
                    TreeNode node = new TreeNode();
                    node.Text = r.Name.ToString();
                    BindChild(node,r.Elements());
                    TreeView1.Nodes.Add(node);
                };
            }
            public void BindChild(TreeNode node,IEnumerable<XElement> xs) 
            {
                foreach (XElement r in xs)
                {
                    TreeNode ChildNode = new TreeNode();
                    ChildNode.Text = r.Name.ToString();
                    node.ChildNodes.Add(ChildNode);
                    BindChild(ChildNode, r.Elements());
                };
            }LINQ绑TREEVIEW