SoFunction
Updated on 2025-03-07

C# How to create XML files in WINForm program

<?xml version="1.0" encoding="gb2312"?>
<FilesInformation>
  <version>1.0.1818.42821</version>
  <description>illustrate</description>
  <FileItem 
  FileName="name"
  FileVersion="sdf"
  FileLength="sdf"
  FileCreationTime="sd"
  />
</FilesInformation>
string path = ;   

Get and set the name of the directory containing the application

(path + XmlFileName) 

It is to determine whether the file exists, and the passed parameters are path + file name

XmlDocument xmlDoc = new XmlDocument();    

This sentence is to create an XmlDocument object

XmlDeclaration xmlSM = ("1.0", "UTF-8", null);   

This sentence is a statement to add the xml file header

(xmlSM); 

This sentence appends the created XmlDocument object to the xml file declaration.

XmlElement DeviceTree = ("DeviceTree"); 

This sentence is to create a node with the label named DeviceTree

("name", "Equipment Tree");

This sentence sets the name attribute of the node to the device tree

(DeviceTree);

This sentence adds the created node to the XmlDocument object that you started to create.

(path + XmlFileName);

Finally, save the created xml file

Method 1:

private void button1_Click(object sender, EventArgs e) 
{     
XmlDocument xmlDoc = new XmlDocument();           //Create an Xml definition declarationXmlDeclaration dec = ("1.0", "utf-8", null);        
(dec);           //Create the root nodeXmlElement root = ("FilesInformation");        
(root);       
XmlElement version = ("version");       = "1.0.1818.42821";     
(version);         
XmlElement description = ("description");     
 = "illustrate";     
(description);       
XmlElement fileItem = ("FileItem");     
("FileName", "name");     
("FileVersion", "xx");     
("FileLength", "xxx");     
("FileCreationTime", "xxxx");     
(fileItem);          
("");   
 }

Method 2:

XmlDocument xmldoc = new XmlDocument();
               XmlText xmltext;
 
               //statement               XmlNode xmlnode = (, "", "");
                += " encoding=\"GB2312\"";
               (xmlnode);
 
               //Add root node               XmlElement xmlelementroot = ("", "Config", "");
               // When the root node contains node text, it will cause confusion in the XML document structure               //xmltext = ("Configuration Information");               //(xmltext);
               (xmlelementroot);
 
               //Add an element               XmlElement xmlelement1 = ("", "DTL", "");
               xmltext = ("2010-10-25");
               (xmltext);
               (1).AppendChild(xmlelement1);
 
               //Add another element               XmlElement xmlelement2 = ("", "DTF", "");
               xmltext = ("2011-02-10");
               (xmltext);
               (1).AppendChild(xmlelement2);
 
               //save               (+\\);

Method 3:

XmlTextWriter xmlwriter = new XmlTextWriter(getPath(), );
                 = ;
                 = 4;
 
                ();
                ("", "Config", "");
 
                ("", "DTL", "");
                ("2010-10-25");
                ();
 
                ("", "DTF", "");
                ("2011-02-10");
                ();
 
                ();
                ();
 
                ();
                ();

getPath() in the above code is a custom method to get the file path and name. Please modify it according to your actual situation! I usually set it

+\\

In general, Method 3 is relatively easy to understand and is simple and easy to use, and it is also my commonly used method!

Hope it will be helpful to you!

The above is the detailed content of how to create XML files in C# in WINForm programs. For more information about creating XML files in c#, please follow my other related articles!