XML learning notes (3)

Source: Internet
Author: User

 Preface

I learned the basic knowledge of XML, and also checked the attributes and methods of the XMLDocument class. Now I want to strengthen it with some online examples.

Subject

Part 1 create XML

Example 1 load XML according to the existing XML document

XmlDocument xmlDoc = new XmlDocument ();
// The load start path starts from the root node, which is the directory where the solution is located.
XmlDoc. Load ("XML \ bookstore. xml ");

 

 Example 2 load XML based on a string in XML format

Code

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. LoadXml (@ "<? Xml version = "" 1.0 "" encoding = "" gb2312 ""?>
<Bookstore>
<Book>
<Title> sqlserver2005 </title>
<Price> 25 </price>
<Bookconcern> 123 </bookconcern>
</Book>
</Bookstore> ");

 

Part 1 read and write XML

Example 1 Add a node

Code

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load ("xml \ bookstore. xml ");
XmlElement newXmlElement = xmlDoc. CreateElement ("book ");
NewXmlElement. SetAttribute ("isNew", "yes ");
XmlNode titleNode = xmlDoc. CreateNode (XmlNodeType. Element, "title ","");
TitleNode. InnerText = "VS2010 ";
NewXmlElement. AppendChild (titleNode );

// Add a plain text section
// XmlNode textnode = xmlDoc. CreateNode (XmlNodeType. Text, "Text ","");
// Textnode. InnerText = "text test ";
// NewXmlElement. AppendChild (textnode );

XmlDoc. DocumentElement. AppendChild (newXmlElement );

Example 2 modify XML and output it as a new file

 

Code

String filename = "New. xml ";
Response. ContentEncoding = System. Text. Encoding. Default;
Response. AppendHeader ("Content-Disposition", "attachment; filename =" + filename );

Response. Clear ();
// XmlDoc content
Response. Write (xmlDoc. OuterXml );
Response. End ();

 

Therefore, the next step is to learn Response.

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.