Xml
Read:
Open a file (assuming Web.config is in the root directory)
String Filename=server.mappath ("/") + @ "Webapplication1\web.config";
XmlDocument xmldoc= New XmlDocument ();
XmlDoc. Load (filename);
Get Top level Node list
XmlNodeList Topm=xmldoc. Documentelement.childnodes;
foreach (xmlelement element in topm)
{
if (element. Name.tolower () = = "appsettings")
{
To get the child nodes 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;
Also here you can modify the value of the element and save it later.
El. attributes["Value"]. Value=this. TextBox2.Text;
}
}
}
}
The
xmldoc. Save (filename);
Adds an element under a node and sets the value:
if (element. Name.tolower () = = "appsettings")
{
XmlElement elem = XmlDoc. CreateElement ("add");
element. AppendChild (Elem);
Elem. innertext= "LTP";
xmldoc. Save (filename);
}
Effect:
LTP
Add an element under one node and add two properties:
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= "a";
Elem. Setattributenode (XA);
Elem. Setattributenode (XA2);
XmlDoc. Save (filename);
}
Effect:
To 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)
{
To get the child nodes of the node
XmlNodeList nodelist=element. ChildNodes;
foreach (XmlElement el in nodelist)//Read element value
{
if (el. attributes["Key"]. Value==itemname)
{
Element. RemoveChild (EL);
}
}//Loop Element
}//Get the Group
}//Cycle Group
Doc. Save (xmlfile); Be sure to keep it, otherwise it won't 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)
{
}
}