加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:00:51 所属栏目:百科 来源:网络整理
导读:[csharp] view plain copy using System; using System.Windows.Forms; using System.Xml; namespace XMLDemo { public partial class FrmDOM:Form public FrmDOM() InitializeComponent(); } private void btnLoad_Click( object sender,EventArgse) { XmlD


[csharp] view plain copy
  1. usingSystem;
  2. usingSystem.Windows.Forms;
  3. usingSystem.Xml;
  4. namespaceXMLDemo
  5. {
  6. publicpartialclassFrmDOM:Form
  7. publicFrmDOM()
  8. InitializeComponent();
  9. }
  10. privatevoidbtnLoad_Click(objectsender,EventArgse)
  11. {
  12. XmlDocumentxmlDoc=newXmlDocument();
  13. xmlDoc.Load("Books.xml");
  14. MessageBox.Show(xmlDoc.InnerXml);
  15. }
  16. voidbtnCreate_Click( //xml文档
  17. XmlDeclarationdec=xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
  18. xmlDoc.AppendChild(dec);
  19. //创建根节点
  20. XmlElementroot=xmlDoc.CreateElement("Books");
  21. xmlDoc.AppendChild(root);
  22. //节点及元素
  23. XmlNodebook=xmlDoc.CreateElement("Book");
  24. XmlElementtitle=GetXmlElement(xmlDoc,"Title","WindowForm");
  25. XmlElementisbn=GetXmlElement(xmlDoc,"ISBN","222221");
  26. XmlElementauthor=GetXmlElement(xmlDoc,"Author","amandag");
  27. XmlElementprice=GetXmlElement(xmlDoc,"Price","128.00");
  28. price.SetAttribute("Unit","¥");
  29. book.AppendChild(title);
  30. book.AppendChild(isbn);
  31. book.AppendChild(author);
  32. book.AppendChild(price);
  33. root.AppendChild(book);
  34. xmlDoc.Save("Books.xml");
  35. 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. MessageBox.Show("数据已插入!");
  43. voidbtnUpdate_Click(//方法1:获取Books//Book节点的第一个子节点
  44. XmlNodeListnodeList=xmlDoc.SelectSingleNode("Books//Book").ChildNodes;
  45. 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. XmlNodenode=xmlDoc.SelectSingleNode("Books//Book[Author="moon"]//Author");
  55. if(node!=null)
  56. node.InnerText="宝贝";
  57. 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. XmlElementxe=(XmlElement)xn;
  63. xe.RemoveAttribute("Unit");
  64. 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. element.InnerText=value;
  68. returnelement;
  69. }
[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. "222221");
  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. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读