Read and write XML all node personal summary

xiaoxiao2021-03-06  53

read:

// Open a file (assuming web.config 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") {

// Get the child's node xmlnodelist nodelist = element.childNodes;

IF (Nodelist.count> 0) {//dropdownlist1.items.clear ();

Foreach (Xmlelement El in nodelist) // read element value {//dropdownlist1.items.add (ittributes ["key"key"].innerxml); //this.TextBox2.text =L.attributes ["key "]. InnerText; this.TextBox2.text = el.attributes ["key"]. Value; this.label1.text = el.attributes ["value"]. Value;

// The same can be modified here, and SAVE is behind.

// el.attributes ["value"]. Value = this.TextBox2.text;

}

}

} Xmldoc.save (filename);

Add an element under 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:

LTP

Add an element under a 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 = "first";

Elem.setttributenode (XA); Elem.SetaTRibutenode (XA2);

XMLDoc.save (filename);} Effect:

// Add 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.comboBOBOX1.TEXT) {

/ / Get the child's child node xmlnodelist nodelist = element.childNodes; Foreach (Xmlelement El in nodelist) // read element value {if (El.attributes ["key"]. Value == itemname) {Element.removeChild (EL }

} // Circulating element} // Get group

} // cycle group

Doc.save (XMLFile); / / must save it, otherwise you don't work

// Screening 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) {

}

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

New Post(0)