建立xml檔案–AccountDB.xml
使用xml檔案存儲頁面資料,用與使用者登入和使用者賬号清單的展示
<?xml version="1.0" encoding="utf-8"?>
<Root>
<LoginAccount>
<Column Name="test" UserGuid="db7e1daa02d17325">
<UserGuid>db7e1daa02d17325</UserGuid>
<UserName>test</UserName>
<NickName>張三</NickName>
<PassWord>c4ca4238a0b923820dcc509a6f75849b</PassWord>
</Column>
<Column Name="admin" UserGuid="dd2d47215257c963">
<UserGuid>dd2d47215257c963</UserGuid>
<UserName>admin</UserName>
<NickName>1</NickName>
<PassWord>c4ca4238a0b923820dcc509a6f75849b</PassWord>
</Column>
</LoginAccount>
<AccountList>
<Column UserGuid="db7e1daa02d17325" AccountID="11">
<AccountGuid>11</AccountGuid>
<ProductName>360</ProductName>
<AccountNumber>asasasa</AccountNumber>
<PassWord>1111</PassWord>
</Column>
<Column UserGuid="db7e1daa02d17325" AccountID="22">
<AccountGuid>22</AccountGuid>
<ProductName>sina</ProductName>
<AccountNumber>saewewew</AccountNumber>
<PassWord>1111</PassWord>
</Column>
<Column UserGuid="db7e1daa02d17325" AccountID="333">
<AccountGuid>333</AccountGuid>
<ProductName>csdn</ProductName>
<AccountNumber>fdfdfdfd</AccountNumber>
<PassWord>2222</PassWord>
</Column>
<Column UserGuid="db7e1daa02d17325" AccountID="4444">
<AccountGuid>4444</AccountGuid>
<ProductName>baidu</ProductName>
<AccountNumber>1111</AccountNumber>
<PassWord>1111</PassWord>
</Column>
</AccountList>
</Root>
建立完成xml檔案以後,設定好自定義的格式,xml檔案的節點名字可以自定義設定,這是xml檔案的優點
在使用者登入頁面需要查詢xml 檔案内部使用者的值
在擷取xml檔案之前先擷取xml檔案的路徑,友善讀取檔案
在web頁面讀取xml檔案路徑方法
private static string FilePath = HttpRuntime.AppDomainAppPath + @"AccountDB.xml";
在win窗體程式讀取xml檔案路徑方法
public static string GetFilePath()
{
DirectoryInfo dir = new DirectoryInfo(Application.StartupPath).Parent.Parent;
string filePath = dir.FullName + "/AccountDB.xml";
return filePath;
}
dir.FullName擷取的是目前站點的路徑
需要添加using System.IO;引用
在操作xml檔案内部的資料時,往往直接在xml内部讀取資料比較麻煩,現在我們可以将xml檔案内部的資料直接轉存在自定義的DataTable内,在C#中,操作DataTable往往比xml快的多(個人觀點)
在讀取上面的AccountDB.xml檔案,可以将讀取出兩張表,我這邊隻讀出使用者登入表
在将xml檔案資料轉換成DataTable之前,我們需要将xml檔案中要轉換的部分讀取出來(不廢話,直接貼代碼)
//擷取目前需要轉換的XML
Public string GetXmlStr()
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(GetFilePath());//這裡的GetFilePath()是擷取目前xml檔案的路徑我這裡是在win窗體程式内應用 web端可以直接調用FilePath
string xmlstr = xmldoc.InnerXml;
int startnum = xmlstr.IndexOf(@"<LoginAccount>");
int endnum = xmlstr.IndexOf(@"</LoginAccount>");
xmlstr = xmlstr.Substring(startnum, endnum - startnum + );//數字15代表</LoginAccount>的長度
return xmlstr ;
}
擷取到需要轉換的xml檔案以後,直接将xml檔案資料轉換成DataTable(這裡可以傳回DataSet,根據自己的需要)
public static DataTable GetLoginList()
{
DataSet xmlDS = new DataSet();
StringReader stream = null;
XmlTextReader reader = null;
try
{
string xmlstr = GetXmlStr();
stream = new StringReader(xmlstr);
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
}
catch (Exception ex)
{
string strTest = ex.Message;
return null;
}
finally
{
if (reader != null)
reader.Close();
}
return xmlDS.Tables[];
}
在将資料轉換成表以後,在登入中隻需要在轉換出的表中查找就可以了
Public bool Login(string username,string pwd)
{
bool bl=false;
DataTable dt=GetLoginList();
DataRow[] daArr=dt.Select("UserName='"+username+"' AND PassWord='"+pwd.MD5EnCode();+"' ");
DataTable account=new DataTable();
account=dt.Copy();
account.Row.Clear();
if(daArr.Length>)
{
for (int i = ; i < daArr.Length; i++)
{
account.ImportRow(daArr[i]);
}
}
bl=account.Rows.Count>;
return bl;
}
上面說到對xml檔案的讀取,使用者登入完成以後一般新使用者也是需要注冊滴,在注冊新使用者時需要在xml寫入新的資料,按照xml檔案格式插入相應的資料節點
在寫入時一樣需要讀取到目前xml檔案并且找到節點,并且在該節點下插入資料節點,直接奉上代碼以供參考(不喜勿噴)
public void RegisterAccount(string username,string nickname,string pwd)
{
string xmlurl=GetFilePath();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlurl);
XmlNode root = xmldoc.SelectSingleNode("Root");//查找<Root>節點
XmlNode loginaccount = root.SelectSingleNode("LoginAccount"); //查找<LoginAccount>節點
XmlElement col = xmldoc.CreateElement("Column");//建立一個<Column>節點
col.SetAttribute("Name", username);
string guid="";//主鍵Guid自動生成,這裡可根據使用者需要設定(擷取Guid方法可以私信我)
XmlElement userguid = xmldoc.CreateElement("UserGuid"); //建立<UserGuid>子節點
userguid.InnerText = guid; //設定節點值
col.AppendChild(userguid);//添加到<Column>節點
XmlElement name = xmldoc.CreateElement("UserName"); //建立<UserName>子節點
name.InnerText = username; //設定節點值
col.AppendChild(name);//添加到<Column>節點
XmlElement nick = xmldoc.CreateElement("NickName"); //建立<NickName>子節點
nick.InnerText = nickname; //設定節點值
col.AppendChild(nick);//添加到<Column>節點
XmlElement password = xmldoc.CreateElement("PassWord"); //建立<PassWord>子節點
password.InnerText = pwd.MD5EnCode(); //設定節點值
col.AppendChild(password);//添加到<Column>節點
loginaccount.AppendChild(col); //将<Column>節點添加到<LoginAccount>節點
xmldoc.Save(xmlurl);//儲存目前xml檔案
}
在插入資料之前可以添加一個小方法,驗證目前添加的資料是否存在
public bool CheckUser(string username)
{
bool bl = true;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(WinHelp.GetFilePath());
XmlNode root = xmldoc.SelectSingleNode("Root");//查找<Root>節點
XmlNode loginaccount = root.SelectSingleNode("LoginAccount"); //查找<LoginAccount>節點
XmlNodeList columnlist = loginaccount.SelectNodes("Column");
foreach (XmlNode colunm in columnlist)
{
XmlElement xm = (XmlElement)colunm;
if (xm.GetAttribute("Name") == username)
{
bl = false;
}
}
return bl;
}
在使用者修改密碼時需要對目前使用者資料在xml裡面進行修改,就需要對Xml檔案進行操作
這裡示範使用者修改密碼
public void UpdateAccount(string oldpwd,string newpwd,string accountguid)
{
string xmlurl=GetFilePath();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlurl);
XmlNode root = xmldoc.SelectSingleNode("Root");//查找<Root>節點
XmlNode loginaccount = root.SelectSingleNode("LoginAccount"); //查找<LoginAccount>節點
XmlNodeList columnlist = loginaccount.SelectNodes("Column");
foreach (XmlNode colunm in columnlist)
{
XmlElement xm = (XmlElement)colunm;
if (xm.GetAttribute("UserGuid") == accountguid)
{
XmlNodeList conxm = xm.ChildNodes;
foreach (XmlNode item in conxm)
{
XmlElement itxe = (XmlElement)item;
string name = itxe.Name;
if(name=="PassWord")
{
if(itxe.InnerText==old.MD5EnCode())
{
itxe.InnerText = newpwd.MD5EnCode();
}
}
}
}
}
xmldoc.Save(xmlurl);
}
在使用者管理時,系統管理者可以将指定的使用者删除,這就需要對xml節點進行删除,删除xml節點并儲存
private void DelAccount(string accountguid)
{
string xmlurl = GetFilePath();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlurl);
XmlNode root = xmldoc.SelectSingleNode("Root");//查找<Root>節點
XmlNode loginaccount = root.SelectSingleNode("LoginAccount"); //查找<LoginAccount>節點
XmlNodeList columnlist = loginaccount.SelectNodes("Column");
foreach (XmlNode colunm in columnlist)
{
XmlElement xm = (XmlElement)colunm;
if (xm.GetAttribute("UserGuid") == accountguid)
{
XmlElement colxml = (XmlElement)loginaccount;
colxml.RemoveChild(colunm);//移除掉<LoginAccount>節點下指定的子節點
}
}
xmldoc.Save(xmlurl);
}
上面在使用者登入,使用者注冊 ,密碼修改,使用者管理等各使用到的是對xml檔案的查詢,新增 ,修改,删除,等基本操作,以上代碼個人親測通過,基本上是copy以後可以直接使用,如有疑問可以私信我,不喜勿噴,
源代碼已上傳,有需要可以下載下傳http://download.csdn.net/detail/shechaojin/9502916