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

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


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

    猜你在找的XML相关文章