zoukankan      html  css  js  c++  java
  • [转]操作xml,将xml数据显示到treeview的C#代码

    XmlDocument xml = new XmlDocument();
            private void Form1_Load(object sender, EventArgs e)
            {
                CreateXML();
                TreeNode tn = new TreeNode("魔兽");
                treeView1.Nodes.Add(tn);
                xml.Load(@"D:XMLFile.xml");
                XmlNode nod = xml.DocumentElement;
                int i = 0;
                foreach (XmlNode xn in nod.ChildNodes)
                {
                    treeView1.TopNode.Nodes.Add(xn.Attributes["two"].Value);
                    foreach (XmlNode xn2 in xn.ChildNodes)
                    {
                        treeView1.TopNode.Nodes[i].Nodes.Add(xn2.InnerText);
                    }
                    i++;
                }
            }
            public void CreateXML()
            {
                //创建xml文件
                XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "utf-8", null);
                xml.AppendChild(dec);
                //添加树形字段
                XmlElement one = xml.CreateElement("one");
                XmlElement two = xml.CreateElement("two");
                XmlElement two1 = xml.CreateElement("two");
                XmlElement three = xml.CreateElement("three");
                XmlElement threeDL = xml.CreateElement("three");
                XmlElement three1 = xml.CreateElement("three");
                XmlElement three10 = xml.CreateElement("three");
                //添加树形结构关系
                xml.AppendChild(one);
                one.AppendChild(two);
                one.AppendChild(two1);
                two.AppendChild(three);
                two.AppendChild(threeDL);
                two1.AppendChild(three1);
                two1.AppendChild(three10);
                //添加属性
                two.SetAttribute("two", "不死");
                two1.SetAttribute("two", "暗夜");
                //添加内容
                three.InnerText = "DK";
                threeDL.InnerText = "DL";
                three1.InnerText = "DH";
                three10.InnerText = "WD";
                xml.Save(@"D:XMLFile.xml");
            }

    转:http://www.jb51.net/article/43892.htm

  • 相关阅读:
    python 第三方库大全
    windows CMD实现的信息收集工具
    ip协议是哪一层的协议
    MetaWeblog访问地址
    通过卸载程序触发恶意进程
    IP地址查询接口
    mysql 密码忘记解决办法
    查询IP地址的免费API
    showdan
    【译】android的审计和hacking工具
  • 原文地址:https://www.cnblogs.com/mvv118/p/3635205.html
Copyright © 2011-2022 走看看