The XmlDocument class is used in C to create and modify data files in XML format

  • 2021-09-16 07:45:36
  • OfStack

Modifying XML document data through the XmlDocument class usually requires several major steps or some of them.

(1) Get an XmlDocument class object that contains XML document data. There are usually two ways to do this:

Create an empty object without any nodes through the constructor of the XmlDocument class, which is commonly used as the default constructor.

(2) Obtain a node (XmlNode type) through ChildNodes and Item attributes of XmlDocument class, and modify the data of the selected node through Name, Value and InnerText attributes of XmlNode.

(3) Create new element nodes and attribute nodes by CreateElement () and CreateAttribute () methods of XmlDocument class, and set the attributes of new nodes by Name, Value, InnerText and other attributes of XmlNode. Common definitions of CreateElement () and CreateAttribute () are as follows.

CreateElement (string name): Creates an element node with the specified qualified name, where name represents the qualified name of the element node and returns an object of type XmlElement.

CreateAttribute (string name): Creates an attribute node with the specified qualified name, where name represents the qualified name of the attribute node and returns an XmlAttribute type object.

(4) Create an XML document description through the CreateXmlDeclaration () method of the XmlDocument class and add it to the XML document through the XmlDocument. AppendChild () method. CreateXmlDeclaration () is defined as follows.

CreateXmlDeclaration (string version, string encoding, string standalone): Creates an XML document description with the specified version and encoding. Among them, version represents the version, encoding represents the encoding format of XML document, which defaults to utf-8, standalone indicates whether to write independent attributes on XML declaration, and yes or no can be selected.

(5) Creates an XML annotation with the specified text through the CreateComment () method of the XmlDocument class and adds it to the XML document through the XmlDocument. AppendChild () method.

CreateComment (string data): Creates an XML annotation containing the specified text, where data represents the text content of the annotation. Returns an XmlComment type object.

(6) Save 1 XML document data to a file or data stream via the Save () method of the XmlDocument class, which contains the following overloaded version:

Save (Stream sr): Saves XML document data in memory to a specified data stream, where sr represents a specific writable data stream.

Save (string filename): Saves the XML document data in memory to the specified file, where filename represents the XML file name.

Save (TextWriter tw): Saves the XML document data in memory to the specified text data writer, where tw represents one text writer object.

Save (XmlWriter xw): Saves the XML document data in memory to the specified XML data writer, where xw represents one XML data writer object.


Simple example
Write to document:


static void Main(string[] args) 
    { 
      XmlDocument doc = new XmlDocument();// Instantiate a document object  
       
      if (File.Exists("student.xml"))// Load the document if the file already exists  
      { 
        doc.Load("student.xml"); 
      } 
      else// Otherwise  
      { 
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8","yes");// Setting declaration  
        doc.AppendChild(dec); 
 
        XmlElement root = doc.CreateElement("root");// Add root node  
        doc.AppendChild(root); 
      } 
 
      XmlElement student = doc.CreateElement("student");// Insert 1 A student Node  
      student.SetAttribute("id", "120");// Settings id Attribute  
      student.SetAttribute("age", "22");// Settings age Attribute  
      student.InnerText = " Zhang 3";// Set intermediate text  
 
      doc.DocumentElement.AppendChild(student);// Will student The node is connected to the root node  
 
      doc.Save("student.xml");// Save a document  
    } 

xml document generated after 3 executions:


<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<root> 
 <student id="120" age="22"> Zhang 3</student> 
 <student id="120" age="22"> Zhang 3</student> 
 <student id="120" age="22"> Zhang 3</student> 
</root> 


Related articles: