asp.net operations xml additions and deletions change sample sharing

Source: Internet
Author: User
Tags foreach

  This article mainly introduces the asp.net operation of XML additions and deletions, the need for friends can refer to the following

The   code is as follows: using system;  using system.collections;  using system.componentmodel;  using system.data;& nbsp 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   private void AddElement () & nbsp {      Loadxml ();      XmlNode xmldocselect=xmldoc.selectsinglenode ("user");      XmlElement el=xmldoc.createelement ("person");    //Add person node       El. SetAttribute ("Name", "Wind and Cloud");    //Add the person node's attribute "name"       el. setattribute ("Sex", "female");    //Add the person node's properties "Sex"       el. SetattribUte ("Age", "25");    //Add the person node's attributes "age"       XmlElement xesub1=xmldoc.createelement ("Pass");    //Add node in person node       XESUB1. innertext= "123";    //Set text node       El. AppendChild (XESUB1);      XmlElement xesub2=xmldoc.createelement ("address");      Xesub2. innertext= "Kunming";    //Set text node       El. AppendChild (XESUB2);      Xmldocselect.appendchild (EL);      Xmldoc.save ( Server.MapPath ("User.xml")); } //Modify node   private void Updateelement ()   {      Loadxml ();      XmlNodeList nodelist=xmldoc.selectsinglenode ("user"). ChildNodes;    //Get all child nodes of bookstore node       foreach (XmlNode xn in nodelist)    //traverse all child nodes   & nbsp   {          XmlElement xe= (XmlElement) xn;    //convert child node type to XmlElement type   & nbsp       if (XE. GetAttribute ("name") = = "Cloud")    //If the Name property value is "cloud"           {              XE. SetAttribute ("name", "invention"); If there are sub nodes below go               XmlNodeList Nls=xe. CHILDNODES;//continues to fetch all child nodes of XE child node               foreach (XmlNode xn1 in NLS)//traverse               {                  XmlElement xe2= (Xmlele ment) xn1;//conversion type                   if (xe2. name== "pass")//If found                   {          &NBS P           Xe2. innertext= "66666"//Then modify                       break;    &N Bsp            }             }              break;         }     }      Xmldoc.save (Ser Ver. MapPath ("User.xml"))//Save  } //delete node   private void Deletenode ()   {      Loadxml (); nbsp     XmlNodeList xnl=xmldoc.selectsinglenode ("user"). childnodes;      foreach (XmlNode xn in xnl)       {          Xmlelem ENT xe= (XmlElement) xn;          if (XE. GetAttribute ("name") = = "invention")           {             //xe. RemoveAttribute ("name");//delete name attribute               XE. RemoveAll ()//Delete all contents of this node               break;         }     }      Xmldoc.save (Server.MapPath ("User.xml"))//save  }  private void Showit () & nbsp {      Loadxml ();      XmLnode xn=xmldoc.selectsinglenode ("user");      XmlNodeList xnl=xn. childnodes;      foreach (XmlNode xnf in xnl)       {          Xmlele ment xe= (XmlElement) xnf;         //Console.WriteLine (XE. GetAttribute ("name"))//Display property value          //Console.WriteLine (XE. GetAttribute ("Sex"));         ////XmlNodeList Xnf1=xe. childnodes;         //foreach (XmlNode xn2 in Xnf1)//          {             //Console.WriteLine (XN2. innertext);             /show sub node text      //}     }&NBSP ; }      XML style:      code as follows: <?xml version= "1.0" encoding= "gb2312"?>  <user>       <person> </person>      <person name= "Wind Pull" sex= "male" age= "&G"t;          <pass>123</pass>          <Address> daming </Address>      </person>      <person name= "The Wind" sex= "female" age= ">"           <pass>123</pass>          <Address> Kunming </ address>      </person>  </user>  
Related Article

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.