XMLDocument对xml文件的增删改查操作.doc
《XMLDocument对xml文件的增删改查操作.doc》由会员分享,可在线阅读,更多相关《XMLDocument对xml文件的增删改查操作.doc(16页珍藏版)》请在冰点文库上搜索。
从结构上讲.XmlElement是XmlNode派生类.所以两者比较没什么意义.
XmlNode为抽像类,不能直接实例化.
已知有一个xml文件(bookstore.xml)如下:
xmlversion="1.0"encoding="gb2312"?
>
Oberon'sLegacyCorets,Eva
5.95
1、往节点中插入一个节点:
XmlDocumentxmlDoc=newXmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNoderoot=xmlDoc.SelectSingleNode("bookstore");//查找
XmlElementxe1=xmlDoc.CreateElement("book");//创建一个节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
XmlElementxesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到节点中
XmlElementxesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElementxesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到节点中
xmlDoc.Save("bookstore.xml");
//===============================================
结果为:
xmlversion="1.0"encoding="gb2312"?
>
Oberon'sLegacyCorets,Eva
5.95
CS从入门到精通候捷
58.3
2、修改节点:
将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点的文本修改为“亚胜”。
XmlNodeListnodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNodexninnodeList)//遍历所有子节点
{
XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
XmlNodeListnls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNodexn1innls)//遍历
{
XmlElementxe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}
xmlDoc.Save("bookstore.xml");//保存。
//==================================================
最后结果为:
xmlversion="1.0"encoding="gb2312"?
>
Oberon'sLegacyCorets,Eva
5.95
CS从入门到精通亚胜
58.3
3、删除节点的genre属性,删除节点。
XmlNodeListxnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNodexninxnl)
{
XmlElementxe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
elseif(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");
//===========================================
最后结果为:
xmlversion="1.0"encoding="gb2312"?
>
Oberon'sLegacyCorets,Eva
5.95
4、显示所有数据。
XmlNodexn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeListxnl=xn.ChildNodes;
foreach(XmlNodexnfinxnl)
{
XmlElementxe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeListxnf1=xe.ChildNodes;
foreach(XmlNodexn2inxnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}
1.已知有一个xml文件(bookstore.xml)如下:
xmlversion="1.0"encoding="gb2312"?
>
Oberon'sLegacy Corets,Eva
5.95
1、往节点中插入一个节点:
XmlDocumentxmlDoc=newXmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNoderoot=xmlDoc.SelectSingleNode("bookstore");//查找
XmlElementxe1=xmlDoc.CreateElement("book");//创建一个节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
XmlElementxesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到节点中
XmlElementxesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElementxesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到节点中
xmlDoc.Save("bookstore.xml");
//================
结果为:
xmlversion="1.0"encoding="gb2312"?
>
Oberon'sLegacy Corets,Eva
5.95
CS从入门到精通 候捷
58.3
2、修改节点:
将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点的文本修改为“亚胜”。
XmlNodeListnodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNodexninnodeList)//遍历所有子节点
{
XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
XmlNodeListnls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNodexn1innls)//遍历
{
XmlElementxe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}
xmlDoc.Save("bookstore.xml");//保存。
//=================
最后结果为:
xmlversion="1.0"encoding="gb2312"?
>
Oberon'sLegacy Corets,Eva
5.95
CS从入门到精通 亚胜
58.3
3、删除节点的genre属性,删除节点。
XmlNodeListxnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNodexninxnl)
{
XmlElementxe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
elseif(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");
//====================
最后结果为:
xmlversion="1.0"encoding="gb2312"?
>
Oberon'sLegacy Corets,Eva
5.95
4、显示所有数据。
XmlNodexn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeListxnl=xn.ChildNodes;
foreach(XmlNodexnfinxnl)
{
XmlElementxe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeListxnf1=xe.ChildNodes;
foreach(XmlNodexn2inxnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}
1.private void btnLoad_Click(object sender, EventArgs e)
2. {
3. XmlDocument xmlDoc = new XmlDocument();
4. xmlDoc.Load("Books.xml");
5. MessageBox.Show(xmlDoc.InnerXml);
6. }
7.
8. private void btnCreate_Click(object sender, EventArgs e)
9. {
10. //xml文档
11. XmlDocument xmlDoc = new XmlDocument();
12. XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
13. xmlDoc.AppendChild(dec);
14.
15. //创建根节点
16. XmlElement root = xmlDoc.CreateElement("Books");
17. xmlDoc.AppendChild(root);
18.
19. //节点及元素
20. XmlNode book = xmlDoc.CreateElement("Book");
21. XmlElement title = GetXmlElement(xmlDoc, "Title", "Window Form");
22. XmlElement isbn = GetXmlElement(xmlDoc, "ISBN", "111111");
23. XmlElement author = GetXmlElement(xmlDoc, "Author", "amandag");
24. XmlElement price = GetXmlElement(xmlDoc, "Price", "128.00");
25. price.SetAttribute("Unit", "¥");
26.
27. book.AppendChild(title);
28. book.AppendChild(isbn);
29. book.AppendChild(author);
30. book.AppendChild(price);
31. root.AppendChild(book);
32.
33. xmlDoc.Save("Books.xml");
34. MessageBox.Show("数据已写入!
");
35. }
36.
37. private void btnInsert_Click(object sender, EventArgs e)
38. {
39. XmlDocument xmlDoc = new XmlDocument();
40. xmlDoc.Load("Books.xml");
41.
42. XmlNode root = xmlDoc.SelectSingleNode("Books");
43.
44. XmlElement book = xmlDoc.CreateElement("Book");
45. XmlElement title = GetXmlElement(xmlDoc, "Title", "ASP.NET");
46. XmlElement isbn = GetXmlElement(xmlDoc, "ISBN", "222222");
47. XmlElement author = GetXmlElement(xmlDoc, "Author", "moon");
48. XmlElement price = GetXmlElement(xmlDoc, "Price", "111.00");
49. price.SetAttribute("Unit", "{1}quot;);
50.
51. book.AppendChild(title);
52. book.AppendChild(isbn);
53. book.AppendChild(author);
54. book.AppendChild(price);
55. root.AppendChild(book);
56.
57. xmlDoc.Save("Books.xml");
58. MessageBox.Show("数据已插入!
");
59. }
60.
61. private void btnUpdate_Click(object sender, EventArgs e)
62. {