Method for editing a web.config file using a web mode

zhaozj2021-02-16  115

First, add controls

A ListBox saves the contents in the AppSettings section. The autopostback property of the control is set to True.

Two TextBoxs are used to fill in the key names and key values.

The three Button are "saved" for saving the modified key value. Add new key. "Delete" deletes the key.

Second, add functions

1. Read the existing configuration. Because Web.config is a document in XML format. So use an XMLDocument object to read it in memory. Then put all the bars in the AppSettings section in an XMLNodeList. Finally, add to ListBox.

void binddata () {

XmlDocument xd = new xmldocument ();

XD.Load (server.mappath ("Web.config"); // Load file

XMLnodelist XNL = Xd.selectNodes ("Configuration / AppSettings / Add"); // Select the section.

Listbox1.items.clear ();

Foreach (XMLNode XN IN XNL) // Add to Listbox

{

ListItem Li = new ListItem ();

Li.Text = xn.attributes ["key"]. Value;

Li.Value = xn.attributes ["value"]. value;

ListBox1.Items.Add (Li);

}

}

2. Modify the data. When an existing configuration item is selected, the two TextBox displays the key names and key values, respectively. The key value can be modified at this time.

Private void cmd_modify_click (Object Sender, System.Eventargs E)

{

XmlDocument xd = new xmldocument ();

XD.Load (Server.MAppath ("Web.Config"));

XMLnodelist XNL = Xd.selectNodes ("Configuration / AppSettings / Add");

Foreach (XMLNode XN IN XNL) // Traverse all keys, modify the corresponding key. {

IF (xn.attributes ["key"]. value == txtkey.text)

Xn.attributes ["value"]. value = txtvalue.text;

}

Xd.save (Server.MAppath ("Web.Config"); // Store the result of the modified file back.

TXTKEY.TEXT = ""; txtvalue.text = ""

Binddata ();

}

3. Add key. Add a new button directly to the two Textbox to fill in the key name and key value.

Private void cmd_add_click (Object Sender, System.Eventargs E)

{

XmlDocument xd = new xmldocument ();

XD.Load (Server.MAppath ("Web.Config"));

XMLNode Father = xd.selectsinglenode ("configuration / appsettings"); // Selected for the parent node

XMLNode XN = Father.FirstChild.clone (); // Create a new byte point.

Xn.attributes ["key"]. Value = txtKey.text;

Xn.attributes ["value"]. value = txtvalue.text;

Father.AppendChild (XN); // Add sub-node

Xd.save (Server.MAppath ("Web.config")); // Store

TXTKEY.TEXT = ""; txtvalue.text = ""

Binddata ();

}

4. Delete keys.

Private void cmd_del_click (Object Sender, System.Eventargs E)

{

XmlDocument xd = new xmldocument ();

XD.Load (Server.MAppath ("Web.Config"));

XMLNode Father = Xd.selectsinglenode ("configuration / appsettings"); // Select the parent node

XMLnodelist XNL = Father.childNodes; // Get a collection of child nodes

Foreach (XMLNode XN IN XNL) // Traversing the sub-node

{

IF (xn.attributes ["key"]. value == txtkey.text) // Find the selected node delete

Father.RemoveChild (XN);

}

Xd.save (Server.MAppath ("Web.config")); // Store

TXTKEY.TEXT = ""; txtvalue.text = ""

Binddata ();

}

5. The page is loaded into an existing setting item when the page is loaded, and the display key value is displayed when an item is selected.

Private Void Page_Load (Object Sender, System.EventArgs E)

{

IF (! ispostback)

BindData (); // Call function Reads Data into listbox

}

Private void SELECTEDEXCHANGED (Object Sender, System.Eventargs E)

{

TXTKEY.TEXT = listbox1.items [listbox1.selected ".Text; txtvalue.text = listbox1.items [listbox1.selected" .value;

}

转载请注明原文地址:https://www.9cbs.com/read-8432.html

New Post(0)