Import of XML files in. net using (including creating XML and Importing XML)

Source: Internet
Author: User

The last time I talked about. net create XML file (a fixed way, a dynamic method), this time record how to import XML file

1. How to import XML files

1) XML file format

2) Style of form page

3) Source code Example

3.1) Create the source code for the XML file

         /// <summary>        ///Flexible mode Templates/// </summary>        /// <param name= "sender" ></param>        /// <param name= "e" ></param>        Private voidBtnmodel1_click (Objectsender, EventArgs E) {XmlTest OMS=NewXmlTest (); //get save XML nameOMs. Name = this. txt_model.text; //determine the XML values 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 a compressed format            if( this. rad_3oxz. Checked) {oms.compress= this. rad_3oxz.            Text; }            if( this. Rad_zip. Checked) {oms.compress= this. Rad_zip.            Text; }            //determine 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); //creating an XML Document ObjectXmlDocument xmldoc =NewXmlDocument ();            XmlText XmlText; //add a declaration paragraph to XMLXmlNode XmlNode = Xmldoc. Createxmldeclaration ("1.0","Utf-8",NULL); Xmldoc.            AppendChild (xmlnode); //Add a root element//Creating an element node modelXmlElement Xmlele = Xmldoc. CreateElement ("","Model",""); //Create a text nodeXmlText = Xmldoc. createTextNode (""); //Create a text node for an element nodeXmlele.            AppendChild (xmltext); //the binding element node belongs to the XML documentXmldoc.            AppendChild (xmlele); //Add a child elementXmlElement Xmlele1 = Xmldoc. CreateElement ("","Config",""); XmlText= Xmldoc. createTextNode (""); xmlele1.            AppendChild (xmltext); //Create an attribute node for an element nodexmlele1. SetAttribute ("ID","1"); //binding Element node config under ModelcofigXmldoc. 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); //to create a child element of the first child elementXmlElement 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 XML as File//determine if the saved path exists            if(! Directory.Exists (application.startuppath +@"\tempate") {directory.createdirectory (application.startuppath+@"\tempate"); }            //save XML as File (tempate under program Path)Xmldoc. Save (application.startuppath +@"\tempate\"+ Oms. Name +". XML"); MessageBox.Show ("Create"+oms. Name +". XML succeeded! ","Information Tip:"); //Xmldoc. Save (oms. name+ ". xml");}

3.2) Import the source code of the XML file

 <summary>///import template Method///</summary>//<param name= "sender" ></param>             <param name= "e" ></param> private void Btntempate_click (object sender, EventArgs E) {            OpenFileDialog Ofg = new OpenFileDialog (); Determine if the saved path exists if (! Directory.Exists (application.startuppath + @ "\tempate")) {//create path directory.creat            EDirectory (application.startuppath + @ "\tempate"); }//set The default open path (project installation path +tempate\) Ofg.            InitialDirectory = Application.startuppath + @ "\tempate"; Set the open title, suffix ofg.            Title = "please Select Import XML file"; Ofg.            Filter = "xml file |*.xml";            String path = "";               Create a Dictionary collection object list<string> listnode = new List<string> ();            dictionary<string, string> dict = new dictionary<string, string> (); Gets the open file path if (ofg. ShowDialog () = = DialOgresult.ok) {//get Open File path (including file Name) path = Ofg.            Filename.tostring (); }//determine if the XML file is selected if (path. Length > 0) {//create an Xml class object and load the file into the object System.Xml.XmlDocument doc = new SYSTEM.XM                L.xmldocument (); Doc.                Load (path); Select the XML root node XmlNode model = Doc.                selectSingleNode ("Model"); List of descendant nodes under root node xmlnodelist config = model.                ChildNodes; Traverse all child nodes (config) foreach under Model (XmlNode p in Config) {//translate the child node of the root node into a                    XmlElement element XmlElement XE = (XmlElement) p; Gets the child node of the XmlElement element xmlnodelist xnl = Xe.                    ChildNodes; Traverse config under all child nodes foreach (XmlNode x in Xnl) {//messagebox.s                        How ("" + x.name); String Chi = X.firstchild.innertExt.                        Replace ("\" "," ");                        Saved in the Dictionary collection (node name, nodes Information) listnode.add (chi); Dict.                    ADD (x.name, chi); }}//load XML save information//get XML file name string tempatename = Path. Substring (path.                LastIndexOf (' \ \ ') + 1);                string[] STRs = Tempatename.split ('. ');                This.txt_Model.Text = strs[0]; Load the vendor name if (dict["factoryname"] = = This.rad_3shape. Text) {this.rad_3shape.                Checked = true; } if (dict["factoryname"] = = This.rad_ruike. Text) {this.rad_ruike.                Checked = true; }//load compression mode if (dict["compressmethod"] = = This.rad_3oxz. Text) {this.rad_3oxz.                Checked = true; } if (dict["compressmethod"] = = This.rad_zip. Text) {this.rad_zip.    Checked = true;            }//load Vendor Name if (dict["upmethod"] = = This.rad_web. Text) {this.rad_web.                Checked = true; } if (dict["upmethod"] = = This.rad_ftp. Text) {THIS.RAD_FTP.                Checked = true; }//import hint MessageBox.Show ("import" + strs[0] + ". XML succeeded!            "," Information Tip: "); }        }

4) Import View Page

Import of XML files in. net using (including creating XML and Importing XML)

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.