Using system; using system. collections; using system. componentmodel; using system. data; using system. drawing; using system. web; using system. web. sessionstate; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. htmlcontrols; using system. XML; private xmldocument xmldoc; // load XML file private void loadxml () {xmldoc = new xmldocument (); xmldoc. load (server. mappath ("user. XML ");} // Add node PR Ivate void addelement () {loadxml (); xmlnode xmldocselect = xmldoc. selectsinglenode ("user"); xmlelement El = xmldoc. createelement ("person"); // Add the person node el. setattribute ("name", ""); // Add the "name" El. setattribute ("sex", "female"); // Add the property "sex" El of the person node. setattribute ("Age", "25"); // Add the property of the person node "Age" xmlelement xesub1 = xmldoc. createelement ("pass"); // Add the node xesub1.innertext = "123" in the person node; // set the text node El. appendchild (xesub1); xmlelement xesub2 = xmldoc. createelement ("Address"); xesub2.innertext = "Kunming"; // set the El. appendchild (xesub2); xmldocselect. appendchild (EL); xmldoc. save (server. mappath ("user. XML ");} // modify the node private void updateelement () {loadxml (); xmlnodelist nodelist = xmldoc. selectsinglenode ("user "). childnodes; // obtain all subnodes of the bookstore node foreach (xmlnode Xn in nodelist) // traverse all subnodes {xmlelement Xe = (XML Element) xn; // convert the subnode type to the xmlelement type if (Xe. getattribute ("name") = "") // if the name attribute value is "" {Xe. setattribute ("name", "invention"); // if the following sub-nodes go down xmlnodelist NLS = Xe. childnodes; // continue to obtain the foreach (xmlnode xn1 in NLS) of all the child nodes of Xe // traverse {xmlelement xe2 = (xmlelement) xn1; // conversion type if (xe2.name = "pass") // If {xe2.innertext = "66666"; // modify break;} xmldoc. save (server. mappath ("user. XML "); // save} // Delete the private void del Node Etenode () {loadxml (); xmlnodelist xnl = xmldoc. selectsinglenode ("user "). childnodes; foreach (xmlnode Xn in xnl) {xmlelement Xe = (xmlelement) xn; If (Xe. getattribute ("name") = "invention") {// Xe. removeattribute ("name"); // delete the name attribute Xe. removeall (); // delete all the content of the node break;} xmldoc. save (server. mappath ("user. XML "); // save} private void showit () {loadxml (); xmlnode xn = xmldoc. selectsinglenode ("user"); xmlnodelist xnl = x N. childnodes; foreach (xmlnode xnf in xnl) {xmlelement Xe = (xmlelement) xnf; // console. writeline (Xe. getattribute ("name"); // display the property value // console. writeline (Xe. getattribute ("sex"); // xmlnodelist xnf1 = Xe. childnodes; // foreach (xmlnode xn2 in xnf1) // {// console. writeline (xn2.innertext); // display the style of the child node text // }}} XML: <? XML version = "1.0" encoding = "gb2312"?> <User> <person> </person> <person name = "" Sex = "male" age = "25"> <pass> 123 </pass> <address> daming </address> </person> <person name = "" Sex = "" age = "25"> <pass> 123 </pass> <address> Kunming </address> </person> </user>