treeview加载XML,其中,Name 为节点的Text,Formel 为节点的Name,Explain 为节点的Tag,并且要能实现增删改的操作。
XML文件:<?xml version="1.0" encoding="utf-8" ?>
<root >
  <BM >
    <Name >COD排放</Name >
    <Formel ></Formel >
    <Explain ></Explain >
    <BM >
      <Name >排放公式一</Name >
      <Formel >a+b</Formel >
      <Explain >2</Explain >
    </BM >
    <BM >
      <Name >排放公式二</Name >
      <Formel >a+b-c</Formel >
      <Explain >2</Explain >
    </BM >
  </BM >
  <BM >
    <Name >SO2排放</Name >
    <Formel >a*(b+c)</Formel >
    <Explain ></Explain >
    <BM >
      <Name >计算公式一</Name >
      <Formel >a*(b+c)-d</Formel >
      <Explain >2</Explain >
    </BM >
    <BM >
      <Name >计算公式二</Name >
      <Formel >a*(b-c)+d</Formel >
      <Explain >2</Explain >
      <BM >
        <Name >计算公式三</Name >
        <Formel >a-b</Formel >
        <Explain >2</Explain >
        <BM >
          <Name >计算公式四</Name >
          <Formel >a+b*c</Formel >
          <Explain >2</Explain >
        </BM >
      </BM >
    </BM >
  </BM >
</root >

解决方案 »

  1.   

    private void populateTreeview()
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Title = "Open XML Document";
        dlg.Filter = "XML Files (*.xml)|*.xml";
        dlg.FileName = Application.StartupPath + "\\..\\..\\example.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();
    }save
    private StreamWriter sr;public void exportToXml(TreeView tv, string filename) 
    {
        sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
        //Write the header
        sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        //Write our root node
        sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
        foreach (TreeNode node in tv.Nodes)
        {
            saveNode(node.Nodes);
        }
        //Close the root node
        sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
        sr.Close();
    }private void saveNode(TreeNodeCollection tnc)
    {
        foreach (TreeNode node in tnc)
        {
            //If we have child nodes, we'll write 
            //a parent node, then iterrate through
            //the children
            if (node.Nodes.Count > 0)
            {
                sr.WriteLine("<" + node.Text + ">");
                saveNode(node.Nodes);
                sr.WriteLine("</" + node.Text + ">");
            } 
            else //No child nodes, so we just write the text
                sr.WriteLine(node.Text);
        }
    }
    修该的话就不用写了吧
    直接删除treenode就可以了
    在treenode的选中事件里做
      

  2.   

    关键要符合上面我说的要求啊,我对XML不熟悉,时间比较紧,请各位帮忙
      

  3.   

    现在将XML加载到treeview已经OK了,但是要想在当前节点下增加一个节点怎么做啊
      

  4.   

    treeview.SelectedNode.Nodes.Add("节点");
    上面节点就是
    treeview.SelectedNode.Parent.Nodes.Add("节点");
      

  5.   

    我是想通过选择treeview的某个节点后,在XML中添加一个节点
      

  6.   

    你在treeview里面增加了以后
    保存的时候 xml里面不就有了吗没有
    好好看我给你的代码吧
    private StreamWriter sr;public void exportToXml(TreeView tv, string filename) 
    {
        sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
        //Write the header
        sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        //Write our root node
        sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
        foreach (TreeNode node in tv.Nodes)
        {
            saveNode(node.Nodes);
        }
        //Close the root node
        sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
        sr.Close();
    }private void saveNode(TreeNodeCollection tnc)
    {
        foreach (TreeNode node in tnc)
        {
            //If we have child nodes, we'll write 
            //a parent node, then iterrate through
            //the children
            if (node.Nodes.Count > 0)
            {
                sr.WriteLine("<" + node.Text + ">");
                saveNode(node.Nodes);
                sr.WriteLine("</" + node.Text + ">");
            } 
            else //No child nodes, so we just write the text
                sr.WriteLine(node.Text);
        }
    }
      

  7.   

    TO:Kamui(湿他就湿他)兄,你的代码我试过了,我要的是这个效果:
    COD排放
    排放公式一
    排放公式二
    SO2排放
    计算公式一
    计算公式二
    计算公式三
    计算公式四
    Name 为节点的Text,Formel 为节点的Name,Explain 为节点的Tag
      

  8.   

    TO:Kamui(湿他就湿他)兄,你的代码我试过了,我要的是这个效果: 
    COD排放
        排放公式一
        排放公式二
    SO2排放
        计算公式一
        计算公式二
            计算公式三
                计算公式四
    Name 为节点的Text,Formel 为节点的Name,Explain 为节点的Tag
      

  9.   

    Visual C#中使用XML之实现DOM 
     http://dev.yesky.com/msdn/319/2502319.shtml  
    C#中操作xml文件(插入节点、修改、删除)
      http://www.qqread.com/csharp/p359862.html  
    C#中操作XML修改完整版  
    http://www.itwis.com/html/net/c/20080219/954.html  
    Manipulate XML data with XPath and XmlDocument (C#)  
    http://www.codeproject.com/KB/cpp/myXPath.aspx  
    使用 Visual C# .NET 通过 XPath 表达式查询 XML 
     http://support.microsoft.com/kb/308333/zh-cn  
    ASP.NET 2.0 中的数据访问--XmlDataSource 
     http://www.itkm.cn/article.asp?id=225   
    使用XMLDataSource简单实现多级下拉菜单 
     http://blog.csdn.net/cui2nd/archive/2005/12/20/557315.aspx  
    XML技术讲座之六 XML中的命名空间
      http://cnw2005.cnw.com.cn/issues/2000/49/4914b.asp  
    C#操作xml之xpath语法 
     http://www.cnblogs.com/amboyna/archive/2007/12/18/1003852.html  
    XML文件操作指南 
     http://www.cnblogs.com/jetz/archive/2005/07/30/203831.html  
      

  10.   

    为什么不自己研究一下呢,看着我给你的那些教程,然后自己试一下不是很好吗
    我以前也没有弄过xml,前几天一个项目使用了xml,所以自己找了一些资料,希望你好好看看,很有用的
    永远是要代码,你还搞什么软件啊,是不是,没有提高你怎么会有钱途呢,
    除非你不打算干这行了,你说是不是呢
    希望可以打醒你
      

  11.   

     private void LoadXmlNode_2(TreeNodeCollection ATreeNodes, XmlNode xmlNodeList)
            {
                string ParentName = string.Empty;
                string Formel = string.Empty;
                string Explain = string.Empty;            XmlNode vXmlNode = xmlNodeList.SelectSingleNode("Name");
                if (vXmlNode != null) ParentName = vXmlNode.InnerText;            vXmlNode = xmlNodeList.SelectSingleNode("Formel");
                if (vXmlNode != null) Formel = vXmlNode.InnerText;            vXmlNode = xmlNodeList.SelectSingleNode("Explain");
                if (vXmlNode != null) Explain = vXmlNode.InnerText;            TreeNode vTreeNode = ATreeNodes.Add(Formel, ParentName);
                vTreeNode.Tag = Explain;
                foreach (XmlNode vTemp in xmlNodeList.SelectNodes("BM"))
                {
                    LoadXmlNode_2(vTreeNode.Nodes, vTemp);
                }
            }
    调用:
     XmlDocument doc = new XmlDocument();
                doc.Load(XmlFile);
                XmlNode vRoot = doc.DocumentElement;            treeView1.Nodes.Clear();
                LoadXmlNode_2(treeView1.Nodes, vRoot);
                treeView1.ExpandAll();
      

  12.   

    Kamui(湿他就湿他)兄,你的代码对我很有用,谢了.
      

  13.   

    非常到位,十分感謝.ericzhangbo1982111 
    (好一个二郎神,你就是中国超人’)如果我有分就全部給你啊.
      

  14.   

    樓主好像完全沒有入門.建議看看<<C#入門經典>>