操作xml,将xml数据显示到treeview的C#代码
时间:2023-01-02 19:56:48
效果:
代码:
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");
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java程序员如何编写更好的单元测试的7个技巧
Unity代码实现序列帧动画播放器
java实现PDF转图片的方法
java使用gzip实现文件解压缩示例
Java IO流 文件传输基础
![](https://img.aspxhome.com/file/2023/0/58070_0s.png)
Java JDK11基于嵌套的访问控制的实现
java 微信随机红包算法代码实例
java编程实现根据EXCEL列名求其索引的方法
Android开发实现SubMenu选项菜单和子菜单示例
![](https://img.aspxhome.com/file/2023/0/114280_0s.gif)
springBoot详解集成Swagger流程
![](https://img.aspxhome.com/file/2023/0/89640_0s.png)
探讨Java中函数是值传递还是引用传递问题
![](https://img.aspxhome.com/file/2023/1/85831_0s.png)
C#软件注册码的实现代码
如何将Mybatis连接到ClickHouse
Java 超详细讲解类的定义方式和对象的实例化
![](https://img.aspxhome.com/file/2023/9/70939_0s.png)
C#遍历文件夹获取指定后缀名文件
![](https://img.aspxhome.com/file/2023/6/129696_0s.jpg)
C# wpf 无边框窗口添加阴影效果的实现
![](https://img.aspxhome.com/file/2023/0/119470_0s.png)
Spring MVC URL地址映射的示例代码
![](https://img.aspxhome.com/file/2023/3/72903_0s.jpg)
C#实现快速排序算法
![](https://img.aspxhome.com/file/2023/8/84478_0s.jpg)
Spring Cloud Gateway 记录请求应答数据日志操作
![](https://img.aspxhome.com/file/2023/3/94193_0s.jpg)