Summary of reading and writing all XML nodes

Source: Internet
Author: User
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)
{}
}

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.