C # winfrom Implementation read Modify xml_c# Tutorial

Source: Internet
Author: User

The example of this article for you to share the Winfrom implementation of reading to modify the XML code for your reference, the specific content as follows

In the Winfrom form, place a text box, 2 buttons, a Panle, as shown below


Form.cs The code in the file:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;


Using System.Xml;
  namespace Xmlconfiger {public partial class Form1:form {public Form1 () {InitializeComponent ();
   public string Path;
  Xmlconfig Xmlconfig; Read XML content private void Button1_Click (object sender, EventArgs e) {OpenFileDialog fileName = new OpenFileDialog () ;//define a File open control Filename.initialdirectory = application.startuppath;//Set open control, the default directory is exe run file folder Filename.filter = "All x ml file |*.
   XML "//Set the file type open by the control Filename.filterindex = 2;//Set the display order of the control open file type Filename.restoredirectory = true;//Set dialog box to remember the previously opened directory if (filename.showdialog () = = DialogResult.OK) {path = FileName.FileName.ToString ();//Get the user-selected full path Name = Pat
     H.substring (Path.lastindexof ("\") + 1)//Get user-selected filename without path xmlconfig = new Xmlconfig (path); int count = Xmlconfig.
   GetCount ();  int ysplit = 30;
     int x1 = 3;
       for (int i = 0; i < count; i++) {Label lb = new label (); Lb. Text = Xmlconfig. GetName (i).
       ToString (); Lb.
       Tag = ""; Lb.
       Size = new System.Drawing.Size (60, 23); Lb.
       AutoSize = false;
       TextBox TB = new TextBox (); Tb. Text = Xmlconfig. Getxmlnode (i).
       ToString (); Tb.
       Tag = i; Lb.
       Location = new Point (x1, I * ysplit); Tb. Location = new Point (x1 + lb.)
       Size.width +, I * ysplit); Panel1.
       Controls.Add (LB); Panel1.
      
     Controls.Add (TB); }}//modify XML content private void Button2_Click (object sender, EventArgs e) {for (int i = 0; i < T His.panel1.Controls.Count; i++) {if (This.panel1.controls[i]. Tag!= null && this.panel1.controls[i].
      Tag.tostring ()!= "") {TextBox textbox1 = (textbox) (This.panel1.controls[i]); Xmlconfig. Savaxmlconfig (Convert.ToInt32) (textbox1. TAG), TextBox1.
     Text); }} xmlconfig. SavacoNfig ();
 }

  
 }
}

Code in

XmlConfig.cs :

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Xml;
Using System.IO;
Using System.Data;


Using System.Windows.Forms;
  Namespace Xmlconfiger {public class Xmlconfig {public int count = 0;
  public string Path= "";
  Private list<string> strlist = new list<string> ();
  Private list<string> listname = new list<string> ();
   The constructor obtains all information public xmlconfig (string Path) {XmlDocument xmldoc = new XmlDocument ();
   Xmldoc.load (path);//Read the specified XML document path = path;
   XmlNode roomlist = Xmldoc.selectsinglenode ("RSS"); XmlNodeList list = Roomlist.
   ChildNodes; foreach (XmlNode item in list) {Listname.add (item. attributes["Name"].
    Value); Strlist. ADD (item.
    InnerText);
   Count = Listname.count;
  }//Get the number of all nodes public int GetCount () {return count;
  Gets the currently returned name public string GetName (int tag) {return Listname[tag] by the tag value; //Gets the value public string GETXM that is currently returned through the tag valueLnode (int tag) {return strlist[tag];
   }//Modify all content in XML the public void Savaconfig () {XmlDocument xmldoc = new XmlDocument ();
   Xmldoc.load (path); XmlNodeList nodelist=xmldoc.selectsinglenode ("RSS"). childnodes;//gets all child nodes for the node for (int i = 0; i < nodelist.count i++)/traverse all child nodes {XmlElement XE = (XmlElement) nod
    Elist[i];
    XmlNode childxml = Nodelist[i]; for (int j = 0; J < Strlist. Count; J + +) {if (listname[j] = = childxml.attributes["Name"]. Value) {XE.
      SetAttribute ("Name", Listname[i]); Xe.
      innertext = Strlist[i];
     Break 
  }} xmldoc.save (path);//save.
  //Modify one of the nodes in the XML node public void savaxmlconfig (int tag, string Name) {Strlist[tag] = Name;
 } 
  


 }
}

XML file:

<?xml version= "1.0" encoding= "Utf-8"?> <rss version= "
2.0" >
 <student name= "name" > Ningzetao </ student>
 <age name= "age" >22</Age>
  
 

The above is the entire content of this article, I hope to help you learn.

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.