XML Chapter III: Editing XML file records
The easiest way to use C # to edit an XML file is, of course, to use a dataset
We continue to use the XML file in the previous article, as follows: <users> <xs:schema id= "users" xmlns= "xmlns:xs=" Http://www.w3.org/2001/XMLSchema "xmlns:msdata=" urn: Schemas-microsoft-com:xml-msdata "> <xs:element name=" Users msdata:isdataset= "true" msdata:locale= "Zh-CN" > <xs:complexType> <xs:choice maxoccurs= "unbounded" > <xs:element name= "User" > <xs:complexType> <xs:sequence> <xs:element name= "UserName" type= "xs:st Ring "minoccurs=" 0 "msdata:ordinal=" 0 "/> <xs:element name=" Userpass "type=" xs:string "minoccurs=" 0 " msdata:ordinal= "1"/> </xs:sequence> <xs:attribute name= "id" type= "xs:string"/> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <user id= "1" > <userName>outrace</userName> <USERPASS&G T;mypass</userpass> </user> ≪user id= "2" > <userName>trace</userName> <userPass>mypass</userPass> </user> &l T;user id= "3" > <username>new user</username> <userpass>new passwd</userpass> </USER&G T;</users>
Then, we create a new XML file that reads as follows: <% @ Import namespace= "System.IO"%><% @ Import namespace= "System.Xml"%><% @ import namespace= "System.Dat" A "%><script language=" C # runat= "Server" >public void Page_Load (Object Src,eventargs e) {/** copyrights: Copyright by SEM IT department* Edition: Version 0.0.1* file: editxml.aspx* Purpose: Edit XML file (modify, delete line) * Author: Ouyang Yun @2005-4-9* Email: Out race@soueast-motor.com* Modification: * * String filename;filename = "Users_2.xml";D ataset myds = new DataSet (); Try{filestream F In; fin = new FileStream (Server.MapPath ("./files/" +filename), FileMode.Open, FileAccess.Read, fileshare.readwrite); Myds.readxml (Fin), fin. Close ();} catch (Exception ex) {Response.Write (ex. message);} Xmlolddata.datasource = Myds.tables[0] before binding modification. Defaultview;olddata.databind (); When the DS is added to the new line DataRow newrow = Myds.tables[0]. NewRow (); newrow["id"] = "n"; newrow["userName" = "test user"; newrow["userpass"] = "Test passwd"; Myds.tables[0]. Rows.Add (NewRow); Binding adds a record to the data Newdata.datasource = Myds.tables[0]. DefaulTview; Newdata.databind (); Filter datarow[] Editrow = myds.tables[0]. Select ("id = ' 44 '"); for (int i=0;i<editrow.length;i++) {editrow[i]["userName"] = "changed user"; editrow[i]["Userpass"] = "changed passwd";} Data Editdata.datasource = Myds.tables[0] After binding modification of records. DefaultView; Editdata.databind (); for (int i=0;i<editrow.length;i++) {editrow[i]. Delete (); Remove the data by deleting}//binding Data Deledata.datasource = Myds.tables[0] After deleting the record. Defaultview;deledata.databind (); Writes the myds changes to the Xmltry{myds.writexml (Server.MapPath ("./files/" +filename), Xmlwritemode.writeschema); catch (Exception ex) {Response.Write (ex. message);} Myds.dispose ();} </script><ptml><pead> <title> Edit Data </title></pead><body> <form runat= " Server "> <table width=" 100% "> <tr> <td> Original data </td> <td> New line </td> <td> Modify rows </td> <td> Delete lines </td> </tr> <tr> <td valign= "Top" ><asp:datagrid id= "Oldda Ta "runat= "Server"/></td> <td valign= "Top" ><asp:datagrid id= "NewData" runat= "Server"/></td> &L T;TD valign= "Top" ><asp:datagrid id= "EditData" runat= "Server"/></td> <td the "top" valign= Atagrid id= "Deledata" runat= "Server"/></td> </tr> </table> </form></body></ptml >After you run this file, you can very intuitive see the operation effect, because we are the last to save, so the content of the XML file has not changed
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.