C#对XML操作:编辑XML文件内容

C#操作XML初步(5)

第三章:XML文件记录的编辑
使用C#来编辑XML文件,最方便的方法当然还是使用DATASET
我们继续使用上一篇中的XML文件,如下:

1<users> <xs:schema id="users" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element msdata:isdataset="true" msdata:locale="zh-CN" name="users"> <xs:complextype> <xs:choice maxoccurs="unbounded"> <xs:element name="user"> <xs:complextype> <xs:sequence> <xs:element minoccurs="0" msdata:ordinal="0" name="userName" type="xs:string"></xs:element> <xs:element minoccurs="0" msdata:ordinal="1" name="userPass" type="xs:string"></xs:element> </xs:sequence> <xs:attribute name="id" type="xs:string"></xs:attribute> </xs:complextype> </xs:element> </xs:choice> </xs:complextype> </xs:element> </xs:schema> <user id="1"> <username>outrace</username> <userpass>mypass</userpass> </user> <user id="2"> <username>trace</username> <userpass>mypass</userpass> </user> <user id="3"> <username>new user</username> <userpass>new passwd</userpass> </user> </users>

则,我们新建一个XML文件,内容如下: @ Import Namespace="System.IO" @ Import Namespace="System.Xml" ``` @ Import Namespace="System.Data"

编辑数据
原数据 新增行 修改行 删除行
``` 大家运行完这个文件,就可以非常直观的看到操作效果,由于我们是最后才保存,所以XML文件的内容没有发生改变
Published At
Categories with Web编程
Tagged with
comments powered by Disqus