Read and write XML all nodes personal summary

Source: Internet
Author: User
Tags add foreach config key string root directory
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)
{

}
}



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.