操作xml,將xml數(shù)據(jù)顯示到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()
{
//創(chuàng)建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");
//添加樹形結(jié)構(gòu)關(guān)系
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", "暗夜");
//添加內(nèi)容
three.InnerText = "DK";
threeDL.InnerText = "DL";
three1.InnerText = "DH";
three10.InnerText = "WD";
xml.Save(@"D:\XMLFile.xml");
}
相關(guān)文章
C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量三
C#語言類型系統(tǒng)提出的一個核心概念裝箱(boxing)拆箱(unboxing)。裝箱和取消裝箱的概念是C#的類型系統(tǒng)的核心。它在“值類型”和“引用類型”之間的架起了一座橋梁,使得任何“值類型”的值都可以轉(zhuǎn)換為object類型的值,反過來轉(zhuǎn)換也可以。2015-10-10
C#實現(xiàn)百度網(wǎng)站收錄和排名查詢功能思路及實例
這篇文章主要介紹了C#實現(xiàn)百度網(wǎng)站收錄和排名查詢功能思路及實例,本文思路同樣適用必應(yīng)、搜狗、搜搜、360等搜索引擎,需要的朋友可以參考下2015-01-01

