使用XmlDocument创建XML文档及增加删除更新节点

前端之家收集整理的这篇文章主要介绍了使用XmlDocument创建XML文档及增加删除更新节点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


[csharp] view plain copy
    @H_502_16@ usingSystem;
  1. usingSystem.Windows.Forms;
  2. usingSystem.Xml;
  3. namespaceXMLDemo
  4. {
  5. publicpartialclassFrmDOM:Form
  6. publicFrmDOM()
  7. InitializeComponent();
  8. }
  9. @H_502_16@
  10. privatevoidbtnLoad_Click(objectsender,EventArgse)
  11. @H_502_16@ {
  12. XmlDocumentxmlDoc=newXmlDocument();
  13. @H_502_16@ xmlDoc.Load("Books.xml");
  14. MessageBox.Show(xmlDoc.InnerXml);
  15. @H_502_16@ }
  16. voidbtnCreate_Click( //xml文档
  17. XmlDeclarationdec=xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
  18. xmlDoc.AppendChild(dec);
  19. //创建根节点
  20. @H_502_16@ XmlElementroot=xmlDoc.CreateElement("Books");
  21. xmlDoc.AppendChild(root);
  22. //节点及元素
  23. @H_502_16@ XmlNodebook=xmlDoc.CreateElement("Book");
  24. XmlElementtitle=GetXmlElement(xmlDoc,"Title","WindowForm");
  25. @H_502_16@ XmlElementisbn=GetXmlElement(xmlDoc,"ISBN","111111");
  26. XmlElementauthor=GetXmlElement(xmlDoc,"Author","amandag");
  27. @H_502_16@ XmlElementprice=GetXmlElement(xmlDoc,"Price","128.00");
  28. price.SetAttribute("Unit","¥");
  29. book.AppendChild(title);
  30. @H_502_16@ book.AppendChild(isbn);
  31. book.AppendChild(author);
  32. @H_502_16@ book.AppendChild(price);
  33. root.AppendChild(book);
  34. xmlDoc.Save("Books.xml");
  35. @H_502_16@ MessageBox.Show("数据已写入!");
  36. voidbtnInsert_Click( XmlNoderoot=xmlDoc.SelectSingleNode("Books");
  37. XmlElementbook=xmlDoc.CreateElement("Book");
  38. "ASP.NET");
  39. "222222");
  40. "moon");
  41. "111.00");
  42. @H_502_16@ MessageBox.Show("数据已插入!");
  43. voidbtnUpdate_Click(//方法1:获取Books//Book节点的第一个子节点
  44. XmlNodeListnodeList=xmlDoc.SelectSingleNode("Books//Book").ChildNodes;
  45. @H_502_16@ XmlElementxe=null;
  46. //遍历所有子节点
  47. foreach(XmlNodexninnodeList)
  48. //将子节点类型转换为XmlElement类型
  49. xe=(XmlElement)xn;
  50. if(xe.Name=="Author"&&xe.InnerText=="amandag")
  51. xe.InnerText="高歌";
  52. if(xe.GetAttribute("Unit")=="¥")
  53. xe.SetAttribute("Unit",0); background-color:inherit">//方法2:
  54. @H_502_16@ XmlNodenode=xmlDoc.SelectSingleNode("Books//Book[Author=\"moon\"]//Author");
  55. if(node!=null)
  56. node.InnerText="宝贝";
  57. @H_502_16@ xmlDoc.Save("Books.xml");
  58. MessageBox.Show("数据已更新!");
  59. voidbtnDelete_Click( XmlDocumentxmlDoc=newXmlDocument();
  60. xmlDoc.Load("Books.xml");
  61. XmlNodeListnodeList=xmlDoc.SelectNodes("Books//Book//Price[@Unit=\"$\"]");
  62. @H_502_16@ XmlElementxe=(XmlElement)xn;
  63. xe.RemoveAttribute("Unit");
  64. @H_502_16@ MessageBox.Show("数据已删除!");
  65. privateXmlElementGetXmlElement(XmlDocumentdoc,153); background-color:inherit; font-weight:bold">stringelementName,153); background-color:inherit; font-weight:bold">stringvalue)
  66. XmlElementelement=doc.CreateElement(elementName);
  67. @H_502_16@ element.InnerText=value;
  68. returnelement;
  69. @H_502_16@ }
[csharp] view plain copy
  1. usingSystem;
  2. usingSystem.Windows.Forms;
  3. usingSystem.Xml;
  4. namespaceXMLDemo
  5. {
  6. classFrmDOM:Form
  7. publicFrmDOM()
  8. InitializeComponent();
  9. }
  10. @H_502_16@
  11. 502_16@ {
  12. newXmlDocument();
  13. @H_502_16@ xmlDoc.Load("Books.xml");
  14. MessageBox.Show(xmlDoc.InnerXml);
  15. @H_502_16@ }
  16. //xml文档
  17. null);
  18. xmlDoc.AppendChild(dec);
  19. //创建根节点
  20. @H_502_16@ XmlElementroot=xmlDoc.CreateElement("Books");
  21. xmlDoc.AppendChild(root);
  22. //节点及元素
  23. @H_502_16@ XmlNodebook=xmlDoc.CreateElement("Book");
  24. "WindowForm");
  25. "111111");
  26. "amandag");
  27. "128.00");
  28. "¥");
  29. book.AppendChild(title);
  30. @H_502_16@ book.AppendChild(isbn);
  31. book.AppendChild(author);
  32. @H_502_16@ book.AppendChild(price);
  33. root.AppendChild(book);
  34. xmlDoc.Save("Books.xml");
  35. @H_502_16@ MessageBox.Show("数据已写入!");
  36. XmlNoderoot=xmlDoc.SelectSingleNode("Books");
  37. XmlElementbook=xmlDoc.CreateElement("Book");
  38. "ASP.NET");
  39. "222222");
  40. "moon");
  41. "111.00");
  42. @H_502_16@ MessageBox.Show("数据已插入!");
  43. //方法1:获取Books//Book节点的第一个子节点
  44. XmlNodeListnodeList=xmlDoc.SelectSingleNode("Books//Book").ChildNodes;
  45. null;
  46. //遍历所有子节点
  47. innodeList)
  48. //将子节点类型转换为XmlElement类型
  49. xe=(XmlElement)xn;
  50. if(xe.Name=="Author"&&xe.InnerText=="amandag")
  51. xe.InnerText="高歌";
  52. if(xe.GetAttribute("Unit")=="¥")
  53. //方法2:
  54. @H_502_16@ XmlNodenode=xmlDoc.SelectSingleNode("Books//Book[Author=\"moon\"]//Author");
  55. null)
  56. node.InnerText="宝贝";
  57. @H_502_16@ xmlDoc.Save("Books.xml");
  58. MessageBox.Show("数据已更新!");
  59. newXmlDocument();
  60. xmlDoc.Load("Books.xml");
  61. XmlNodeListnodeList=xmlDoc.SelectNodes("Books//Book//Price[@Unit=\"$\"]");
  62. @H_502_16@ XmlElementxe=(XmlElement)xn;
  63. xe.RemoveAttribute("Unit");
  64. @H_502_16@ MessageBox.Show("数据已删除!");
  65. stringvalue)
  66. XmlElementelement=doc.CreateElement(elementName);
  67. @H_502_16@ element.InnerText=value;
  68. returnelement;
  69. @H_502_16@ }

猜你在找的XML相关文章