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.