. Net XML operation instructions

Source: Internet
Author: User
Tags net xml
  1. Using system. xml;
  2. // Initialize an XML instance
  3. Xmldocument xml = new xmldocument ();
  4. // Import the specified XML file
  5. XML. Load (PATH );
  6. XML. Load (httpcontext. Current. server. mappath ("~ /File/bookstore. xml "));
  7. // Specify a node
  8. Xmlnode root = xml. selectsinglenode ("/root ");
  9. // Obtain all direct subnodes under a node
  10. Xmlnodelist childlist = root. childnodes;
  11. // Determine whether a subnode exists under the node
  12. Root. haschildnodes;
  13. // Obtain the set of nodes with the same name as the peer node
  14. Xmlnodelist nodelist = xml. selectnodes ("/root/News ");
  15. // Generate a new node
  16. Xmlelement node = xml. createelement ("news ");
  17. // Add a node to a specified node as its subnode
  18. Root. appendchild (node );
  19. // Add the node to a subnode under the specified Node
  20. Root. insertbefore (node, root. childenodes [I]);
  21. // Create a property for the specified node and assign a value to it
  22. Node. setattribute ("ID", "11111 ");
  23. // Add a subnode to a specified Node
  24. Root. appendchild (node );
  25. // Obtain the specified attribute value of a specified Node
  26. String id = node. attributes ["ID"]. value;
  27. // Obtain the text in the specified Node
  28. String content = node. innertext;
  29. // Save the XML file
  30. String Path = server. mappath ("~ /File/bookstore. xml ");
  31. XML. Save (PATH );
  32. // Or use: XML. Save (httpcontext. Current. server. mappath ("~ /File/bookstore. xml "));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.