<?xml version="1.0"?> <UserLogin> <User> <UserCode>001</UserCode> <UserName>操作员1</UserName> <UserPwd>111</UserPwd> </User> <User> <UserCode>002</UserCode> <UserName>操作员2</UserName> <UserPwd>222</UserPwd> </User> </UserLogin>
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml; /** <summary> /// Xml文件的读写类 /// </summary> /// public class XmlRW ...{ public XmlRW() ...{ // // TODO: 在此处添加构造函数逻辑 // } //WriteXml 完成对User的添加操作 //FileName 当前xml文件的存放位置 //UserCode 欲添加用户的编码 //UserName 欲添加用户的姓名 //UserPassword 欲添加用户的密码 public void WriteXML(string FileName,string UserCode,string UserName,string UserPassword) ...{ //初始化XML文档操作类 XmlDocument myDoc = new XmlDocument(); //加载XML文件 myDoc.Load(FileName); //添加元素--UserCode XmlElement ele = myDoc.CreateElement("UserCode"); XmlText text = myDoc.CreateTextNode(UserCode); //添加元素--UserName XmlElement ele1 = myDoc.CreateElement("UserName"); XmlText text1 = myDoc.CreateTextNode(UserName); //添加元素--UserPwd XmlElement ele2 = myDoc.CreateElement("UserPwd"); XmlText text2 = myDoc.CreateTextNode(UserPassword); //添加节点 User要对应我们xml文件中的节点名字 XmlNode newElem = myDoc.CreateNode("element", "User", ""); //在节点中添加元素 newElem.AppendChild(ele); newElem.LastChild.AppendChild(text); newElem.AppendChild(ele1); newElem.LastChild.AppendChild(text1); newElem.AppendChild(ele2); newElem.LastChild.AppendChild(text2); //将节点添加到文档中 XmlElement root = myDoc.DocumentElement; root.AppendChild(newElem); //保存 myDoc.Save(FileName); } //DeleteNode 完成对User的添加操作 //FileName 当前xml文件的存放位置 //UserCode 欲添加用户的编码 public void DeleteNode(string FileName, string UserCode) ...{ //初始化XML文档操作类 XmlDocument myDoc = new XmlDocument(); //加载XML文件 myDoc.Load(FileName); //搜索指定某列,一般是主键列 XmlNodeList myNode = myDoc.SelectNodes("//UserCode"); //判断是否有这个节点 if (!(myNode == null)) ...{ //遍历节点,找到符合条件的元素 foreach (XmlNode xn in myNode) ...{ if (xn.InnerXml == UserCode) //删除元素的父节点 xn.ParentNode.ParentNode.RemoveChild(xn.ParentNode); } } //保存 myDoc.Save(FileName); } //WriteXml 完成对User的修改密码操作 //FileName 当前xml文件的存放位置 //UserCode 欲操作用户的编码 //UserPassword 欲修改用户的密码 public void UpdateXML(string FileName, string UserCode, string UserPassword) ...{ //初始化XML文档操作类 XmlDocument myDoc = new XmlDocument(); //加载XML文件 myDoc.Load(FileName); //搜索指定的节点 System.Xml.XmlNodeList nodes = myDoc.SelectNodes("//User"); if (nodes != null) ...{ foreach (System.Xml.XmlNode xn in nodes) ...{ if (xn.SelectSingleNode("UserCode").InnerText == UserCode) ...{ xn.SelectSingleNode("UserPwd").InnerText = UserPassword; } } } myDoc.Save(FileName); } }
·使用XmlTextWriter写入XML方法 private void xmlwriter() { XmlTextWriter writer= new XmlTextWriter(Server.MapPath("phone4.xml"),null); writer.Formatting = Formatting.Indented; //缩进格式 writer.Indentation =4; writer.WriteStartDocument(); writer.WriteStartElement("Person"); writer.WriteStartAttribute("ID",null); writer.WriteString("004"); writer.WriteEndAttribute(); writer.WriteStartElement("Name"); writer.WriteString("testWriterName"); writer.WriteEndElement(); writer.WriteStartElement("Number"); writer.WriteString("88888"); writer.WriteEndElement(); writer.WriteStartElement("City"); writer.WriteString("testWriterCity"); writer.WriteEndElement(); writer.Flush(); writer.Close(); } ·使用XmlTextReader读取XML方法 private void xmlread() { XmlTextReader reader = new XmlTextReader(Server.MapPath("phone.xml")); while(reader.Read()) { if(reader.LocalName.Equals("Name") || reader.LocalName.Equals("Number")) { this.Label1.Text += reader.ReadString()+"\t"; }