国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

操作xml,將xml數據顯示到treeview的C#代碼

2020-01-24 03:03:08
字體:
來源:轉載
供稿:網友

效果:

代碼:

復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 穆棱市| 灌阳县| 民乐县| 社旗县| 呼图壁县| 宜兰市| 海兴县| 元朗区| 时尚| 隆子县| 临清市| 阿城市| 安福县| 同心县| 依安县| 南溪县| 壶关县| 阳信县| 平阴县| 蒲城县| 凯里市| 亚东县| 夏邑县| 东港市| 柏乡县| 乐都县| 赫章县| 大埔县| 朝阳市| 罗定市| 微山县| 阿坝县| 茂名市| 通榆县| 蒙自县| 武夷山市| 丹阳市| 大城县| 栾城县| 怀来县| 吉木乃县|