Read:
// Open a file (assuming that web. config is in the root directory)
String filename = server. mappath ("/") + @ "webapplication1 \ WEB. config ";
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (filename); // obtain the list of top-level nodes.
Xmlnodelist topm = xmldoc. documentelement. childnodes;
Foreach (xmlelement element in topm)
{
If (element. Name. tolower () = "appsettings ")
{// Obtain the subnode of the node
Xmlnodelist nodelist = element. childnodes; If (nodelist. Count> 0)
{
// Dropdownlist1.items. Clear (); foreach (xmlelement El in nodelist) // read element value
{
// Dropdownlist1.items. Add (El. attributes ["key"]. innerxml );
// This. textbox2.text = El. attributes ["key"]. innertext;
This. textbox2.text = El. attributes ["key"]. value;
This. label1.text = El. attributes ["value"]. value; // you can modify the element value here and save it later. // El. attributes ["value"]. value = This. textbox2.text;
}}}}
Xmldoc. Save (filename );
Add an element to a node and set the value: If (element. Name. tolower () = "appsettings ")
{Xmlelement ELEM = xmldoc. createelement ("add ");
Element. appendchild (ELEM );
ELEM. innertext = "LTP"; xmldoc. Save (filename );
} Effect: <deleettings>
<Add key = "password" value = "admin"/>
<Add> LTP </Add>
</Appsettings>
Add an element under a node and add two attributes: If (element. Name. tolower () = "appsettings ")
{Xmlelement ELEM = xmldoc. createelement ("add ");
Element. appendchild (ELEM); xmlattribute XA = xmldoc. createattribute ("key ");
Xa. value = "LTP"; xmlattribute xa2 = xmldoc. createattribute ("value ");
Xa2.value = "first"; ELEM. setattributenode (xa );
ELEM. setattributenode (xa2); xmldoc. Save (filename );
} Effect: <deleettings>
<Add key = "password" value = "admin"/>
<Add key = "LTP" value = "first"/>
</Appsettings>
// Add an empty element: Xmlnode node = Doc. createelement (groupname );
Node. innertext = "";
Doc. lastchild. appendchild (node); Doc. Save (xmlfile );
Delete a node Element String itemname = This. listbox1.selecteditem. tostring ();
This. listbox1.items. Remove (this. listbox1.selecteditem); // begin del xmlfile
Xmldocument Doc = new xmldocument ();
Doc. Load (xmlfile );
Xmlnodelist topm = Doc. documentelement. childnodes;
Foreach (xmlelement element in topm)
{
If (element. Name = This. combobox1.text)
{
// Obtain the subnode of the node
Xmlnodelist nodelist = element. childnodes;
Foreach (xmlelement El in nodelist) // read the element value
{
If (El. attributes ["key"]. value = itemname)
{
Element. removechild (EL );
} // Cyclic Element
} // Get group} // circular group Doc. Save (xmlfile); // be sure to save it; otherwise, it will not work.
// Filter data Private void reader_xml (string pathflie)
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (pathflie );
Xmlnodelist record1 = xmldoc. documentelement. selectnodes (code [@ ID = '1'])
Int f = 0;
Foreach (xmlnode xnode in record1)
{}
}