操作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)文章
關(guān)于C#中yield關(guān)鍵字的深入解析
這篇文章主要給大家介紹了關(guān)于C#中yield關(guān)鍵字的深入解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11C# AE之返回上一級(jí)和下一級(jí)的實(shí)戰(zhàn)操作
這篇文章主要介紹了C# AE之返回上一級(jí)和下一級(jí)的實(shí)戰(zhàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效
這篇文章主要為大家詳細(xì)介紹了unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01C# 創(chuàng)建控制臺(tái)應(yīng)用程序
這篇文章主要介紹了C# 創(chuàng)建控制臺(tái)應(yīng)用程序,在學(xué)習(xí)C#語(yǔ)言的時(shí)候,首先要學(xué)習(xí)控制臺(tái)的應(yīng)用程序,這樣才能專注于語(yǔ)言的學(xué)習(xí),減少學(xué)習(xí)的梯度,也有利于輸出自己需要輸出的內(nèi)容,一定要先使用控制臺(tái)的應(yīng)用程序的方式,下面就和小編一起學(xué)習(xí)該內(nèi)容吧2021-10-10Unity實(shí)現(xiàn)顏色漸變滑動(dòng)條
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)顏色漸變滑動(dòng)條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07