Two ways to create XML files in. Net
Method 1: Build the XML document in step-by-piece with XML structure, save the file (Dynamic mode)
Method 2: Load the XML structure directly, save the file (fixed mode)
Method 1: Create an XML document dynamically
Build the XML document structure based on the values passed
1. Create an entity class to save the values passed by the form
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace createxmltest.model{public class XmlTest { //xml document name public string name {get; set;} Vendor name Public string Factory {get; set;} Upload method Public string Upmethod {get; set;} Compression mode public string Compress {get; set;}} }
2. Create a form page
3, fixed-mode and flexible source code
Using createxmltest.model;using system;using system.collections.generic;using system.componentmodel;using System.data;using system.drawing;using system.linq;using system.text;using system.threading.tasks;using system.windows.forms;using system.xml;namespace createxmltest{public partial class Form1:form {public for M1 () {InitializeComponent (); }///<summary>//fixed template///</summary>//<param name= "Sender" ></para m>//<param name= "E" ></param> private void Btnmodel_click (object sender, EventArgs e) {//Create an empty XML document XmlDocument xmldoc = new XmlDocument (); XmlDoc. LOADXML ("<?xml version= ' 1.0 ' encoding= ' utf-8 '?>" + "<bookstore>" + "<book>" + "<name> I'm not alone fighting </name>" + "<author> Wu Ching </author>" + "&L t;price>99.8 Yuan </price>" + "</book>" + "</bookstore>"); XmlDoc. Save ("Test 1.xml"); }///<summary>///Flexible mode Templates///</summary>//<param name= "Sender" ></para m>//<param name= "E" ></param> private void Btnmodel1_click (object sender, EventArgs e) {XmlTest oms = new XmlTest (); Gets the save XML name for OMS. Name = This.txt_Model.Text; Determine the XML value that the vendor holds if (This.rad_3shape. Checked) {OMS. Factory =this.rad_3shape. Text; } if (This.rad_ruike. Checked) {OMS. Factory = This.rad_ruike. Text; }//Determine the XML value saved in the compressed format if (THIS.RAD_3OXZ. Checked) {oms.compress = This.rad_3oxz. Text; } if (This.rad_zip. Checked) {oms.compress = This.rad_zip. Text; }//Judge the saved XML-Value if (this.rad_web. Checked) {OMS. Upmethod = This.rad_web. Text; } if (this.rad_ftp.Checked) {oms. Upmethod = This.rad_ftp.Text; }//messagebox.show ("Manufacturer name:" +oms. Factory + ", compression format:" +oms.compress+ ", Upload method:" +oms. Upmethod); Create an XML Document object XmlDocument xmldoc = new XmlDocument (); XmlText XmlText; Add the Declaration of the XML paragraph XmlNode XmlNode = xmldoc. Createxmldeclaration ("1.0", "Utf-8", null); XmlDoc. AppendChild (XmlNode); Add a root element//Create an element node Modelconfig XmlElement Xmlele = xmldoc. CreateElement ("", "Modelconfig", ""); Create text node xmltext = xmldoc. createTextNode (""); Creates the text node Xmlele of the element node. AppendChild (xmltext); The binding element node belongs to the XML document XMLDOC. AppendChild (Xmlele); Add a child element XmlElement Xmlele1 = xmldoc. CreateElement ("", "Config", ""); XmlText = xmldoc. createTextNode (""); Xmlele1. AppendChild (xmltext); Creates an element node for the attribute node xmlele1. SetAttribute ("id", "1"); The binding Element node config is xmldoc under Modelcofig. Childnodes.item (1). AppendChild (XMLELE1); Add a second child element XmlElement Xmlele11 = xmldoc. CreateElement ("", "Config", ""); XmlText = xmldoc. createTextNode ("Configuration 2"); Xmlele11. AppendChild (xmltext); Xmlele11. SetAttribute ("id", "2"); XmlDoc. Childnodes.item (1). AppendChild (XMLELE11); Creates a child element of the first child element XmlElement Xmlele2 = xmldoc. CreateElement ("", "Factoryname", ""); XmlText = xmldoc. createTextNode (OMS. Factory); Xmlele2. AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (Xmlele1). AppendChild (Xmlele2); XmlElement xmlele3 = xmldoc. CreateElement ("", "Compressmethod", ""); XmlText = xmldoc. createTextNode (oms.compress); Xmlele3. AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (Xmlele1). AppendChild (XMLELE3); XmlElement xmlele4 = xmldoc. CreateElement ("", "Upmethod", ""); XmlText = xmldoc. createTextNode (OMS. Upmethod); Xmlele4. AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (Xmlele1). AppendChild (XMLELE4); Save the XML as a file xmldoc. Save (OMS. name+ ". xml"); } }}
4. Fixed creation of XML documents and flexible creation of XML document views
1) The generated XML document is under the bin of the project
2) Test Template 1 result view (fixed)
3) Test Template 2 Results view (dynamic)
Reference Source:
Http://www.cnblogs.com/jhxk/articles/1872930.html
Two ways to create XML files in. Net