XML文件修改
发布网友
发布时间:2022-04-23 06:03
我来回答
共1个回答
热心网友
时间:2022-05-18 07:48
// 假定TreeView控件的id为treeView
XmlDocument dom = new XmlDocument();
dom.Load("aaa.xml");//装载XML文档
//遍历所有节点
int num = 0;
foreach(XmlElement birthday in dom.DocumentElement.ChildNodes)
{
//读取数据
string type = birthday.SelectSingleNode("type").InnerText;
string date = birthday.SelectSingleNode("date").InnerText;
string title = birthday.SelectSingleNode("title").InnerText;
string name = birthday.SelectSingleNode("name").InnerText;
string text = name + ":" + title;//节点文字
string image = type;//节点图片
string data = num.ToString();//节点对应数据
num++;
//装载示例,将新建的节点添加到TreeView
TreeNode node = new TreeNode(text, data, image);//create a new node
treeView.Nodes.Add(node);
//编辑示例:将当前节点的生日更改为当前日期
birthday.SelectSingleNode("date").InnerText = DateTime.Now.ToString();
//删除示例:将当前节点删除
birthday.ParentNode.RemoveChild(birthday);
}
dom.Save();