XML的操作
//增加新会员 0表成功,-1表已存在同名会员
userlist.xml的XML文档,其格式如下:
<?xml version="1.0" encoding="utf-8" ?>
<userlist>
<user>
<id>Samuel</id>
<pass>111</pass>
<nickname>Samuel</nickname>
<tel>123456789</tel>
<email>email@email.com</email>
<address>重庆大学</address>
</user>
</userlist>
UserList类将用DOM访问userlist.xml文档(关于DOM,请参阅第三章),并向外提供以下方法:
1). UserVerify方法,用于会员验证。
2). UserAdd方法,用于注册新会员。
3). UserDelete方法,用于删除会员。
4). UserChangePass方法,用于修改单个会员的密码。
在Solution Explorer中右击UserMan工程àAddàAdd New ItemàClass,取名UserList.cs,打开。UserList.cs的代码如下:
using System;
using System.Xml;
namespace UserMan{
public class UserList{
public UserList(){
//构造函数
}
//会员登录 返回0表成功, -1表会员不存在 –2表密码错
public int UserVerify( string sID, string sPass ){
XmlDocument doc = new XmlDocument();
XmlNode node;
XmlElement rootNode;
string sSelect;
doc.Load("c://InetPub//wwwroot//UserMan//userlist.xml"); //装入XML文档
rootNode = doc.DocumentElement; //rootNode指向userlist根结点
sSelect = "descendant::user[id = '" + sID + "' ]";
//node指向ID = sID的user结点
node= rootNode.SelectSingleNode(sSelect);
if (node == null)
return -1; //没找到,说明会员不存在
node = node.ChildNodes[1].ChildNodes[0];
//node指向该user结点的pass结点的文本结点
if ( node.Value != sPass )
return -2; //密码错
return 0; //登录成功
}
public int UserAdd(string sID, string sPass, string sNickname, string sTel,
string sEmail, string sAddress){
XmlDocument doc = new XmlDocument();
XmlNode node;
XmlElement rootNode;
string sSelect;
doc.Load("c://InetPub//wwwroot//UserMan//userlist.xml");
rootNode = doc.DocumentElement;
sSelect = "descendant::user[id = '" + sID + "' ]";
node= rootNode.SelectSingleNode(sSelect);
if (node != null)
return -1; //已存在同名会员
XmlElement elem = doc.CreateElement( "user" ); //新建一个user元素
//注意标记后的空格
string sXml = "<id>" + sID + "</id> " + "<pass>" + sPass + "</pass> " +
"<nickname>" + sNickname + "</nickname> " + "<tel>" + sTel + "</tel> " + "<email>" + sEmail +
"</email> " + "<address>" + sAddress + "</address> ";
elem.InnerXml = sXml; //设定该user元素的内容
rootNode.AppendChild( elem ); //将新加的user元素作为根结点的子结点
doc.Save( "c://InetPub//wwwroot//UserMan//userlist.xml" ); //存盘
return 0;
}
//删除会员 0表成功,-1表会员不存在 –2表密码错
public int UserDelete( string sID, string sPass){
XmlDocument doc = new XmlDocument();
XmlNode node, nodeDelete;
XmlElement rootNode;
string sSelect;
doc.Load("c://InetPub//wwwroot//UserMan//userlist.xml");
rootNode = doc.DocumentElement;
sSelect = "descendant::user[id = '" + sID + "' ]";
//nodeDelete指向准备删除的结点
nodeDelete = node = rootNode.SelectSingleNode(sSelect);
if (node == null)
return -1; //返回会员不存在错误
node = node.ChildNodes[1];
if ( node.InnerText != sPass )
return -2; //密码错
rootNode.RemoveChild( nodeDelete ); //删除该user结点
doc.Save( "c://InetPub//wwwroot//UserMan//userlist.xml" ); //存盘
return 0;
}
//---------------------------------------------------------------------
//修改密码 0表成功 –1表会员不存在 –2表密码错
public int UserChangePass( string sID, string sOldPass, string sNewPass ){
XmlDocument doc = new XmlDocument();
XmlNode node;
XmlElement rootNode;
string sSelect;
doc.Load("c://InetPub//wwwroot//UserMan//userlist.xml");
rootNode = doc.DocumentElement;
sSelect = "descendant::user[id = '" + sID + "' ]";
node= rootNode.SelectSingleNode(sSelect);
if (node == null)
return -1; //返回会员不存在错误
node = node.ChildNodes[1];
if ( node.InnerText != sOldPass )
return -2; //密码错
node.InnerText = sNewPass; //修改密码
doc.Save( "c://InetPub//wwwroot//UserMan//userlist.xml" ); //存盘
return 0;
}
}
}