如何操作XML文件
首先你得有个XML文件 放在DEBUG文件夹下
bookstore.xml
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
几个方法如下
private void button1_Click(object sender, EventArgs e)
{ //修改XML文件 节点 方法XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{ XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型if (xe.GetAttribute("genre") == "李赞红")//如果genre属性值为“李赞红”
{xe.SetAttribute("genre", "update李赞红");//则修改该属性为“update李赞红”
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点foreach (XmlNode xn1 in nls)//遍历
{ XmlElement xe2 = (XmlElement)xn1;//转换类型if (xe2.Name == "author")//如果找到
{xe2.InnerText = "亚胜";//则修改
break;//找到退出来就可以了
}
}
break;}
}
xmlDoc.Save("bookstore.xml");//保存。
}
private void timer1_Tick(object sender, EventArgs e)
{ Class1 c1 = new Class1(); t = t + 1;//得到总的毫秒数 this.label1.Text = c1.GetAllTime(t);}
private void button2_Click(object sender, EventArgs e)
{ //插入XML 节点方法 XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("bookstore.xml");XmlNode root = xmldoc.SelectSingleNode("bookstore"); //查找<bookstore>
/* * public System.Xml.XmlNode SelectSingleNode(string xpath) System.Xml.XmlNode 的成员 摘要: 选择匹配 XPath 表达式的第一个 XmlNode。 参数: xpath: XPath 表达式。 返回值: 与 XPath 查询匹配的第一个 XmlNode;如果未找到任何匹配节点,则为null。 * 不应该要求将 XmlNode “实时”连接到 XML 文档。 * 也就是说,XML 文档中的更改不会出现在 XmlNode 中,反之亦然。 * * * **/XmlElement xe1 = xmldoc.CreateElement("book");//创建一个<book>节点
/* * public System.Xml.XmlElement CreateElement(string name) System.Xml.XmlDocument 的成员 摘要: 创建具有指定名称的元素。 参数: name: 元素的限定名。如果名称包含冒号, * 则 System.Xml.XmlNode.Prefix 属性反映名称中位于冒号之前的部分, * System.Xml.XmlDocument.LocalName 属性反映名称中位于冒号之后的部分。 * 限定名不能包含“xmlns”前缀。 返回值: 新的 XmlElement。 * **/xe1.SetAttribute("genre", "李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性
XmlElement xesub1 = xmldoc.CreateElement("title");xesub1.InnerText = "CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中 XmlElement xesub2 = xmldoc.CreateElement("author"); xesub2.InnerText = "候捷";xe1.AppendChild(xesub2);
XmlElement xesub3 = xmldoc.CreateElement("price"); xesub3.InnerText = "58.3";xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<bookstore>节点中 xmldoc.Save("bookstore.xml");}
private void button3_Click(object sender, EventArgs e)
{ //删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性, //删除 <book genre="update李赞红" ISBN="2-3631-4">节点。 XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;foreach (XmlNode xn in xnl)
{XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "fantasy")
{xe.RemoveAttribute("genre");//删除genre属性
}
else if (xe.GetAttribute("genre") == "update李赞红")
{ xe.RemoveAll();//删除该节点的全部内容}
}
xmlDoc.Save("bookstore.xml");}
private void button4_Click(object sender, EventArgs e)
{ //现实所有数据 XmlNode xn = xmlDoc.SelectSingleNode("bookstore");XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{XmlElement xe = (XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));XmlNodeList xnf1 = xe.ChildNodes;
foreach (XmlNode xn2 in xnf1)
{ Console.WriteLine(xn2.InnerText);//显示子节点点文本}
}
}
OK了