<?xml version="1.0" encoding="utf-8"?>
<users>
<person name="小米" sex="男">
<tel>15859214524</tel>
<address>福建</address>
<pass>123</pass>
</person>
<person name="小明" sex="男">
<tel>15859214520</tel>
<address>北京</address>
<pass>123</pass>
</person>
<person name="小丽" sex="女">
<tel>13860632541</tel>
<address>上海</address>
<pass>1234</pass>
</person>
<person name="小晓" sex="女">
<tel>13860630541</tel>
<address>广州</address>
<pass>1234</pass>
</person>
<person name="小鱼" sex="男">
<tel>138606002541</tel>
<address>云南</address>
<pass>1234</pass>
</person>
</users>
#region 查找信息
protected string searchInfor(string name)
{
string result = "";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("./xmlPerson.xml")); XmlNode rootNode = xmlDoc.SelectSingleNode("users"); if (rootNode != null)
{
foreach (XmlNode node in rootNode.ChildNodes)
{
if (node.Attributes["name"].Value == name)
{
result += "name:" + node.Attributes["name"].Value + "<br />";
result += "sex:" + node.Attributes["sex"].Value + "<br />";
result += "tel:" + node.ChildNodes[0].InnerText + "<br />";
result += "address:" + node.ChildNodes[1].InnerText + "<br />";
result += "pass:" + node.ChildNodes[2].InnerText + "<br /><br />";
}
}
}
else
{
result = "没找到此条目";
}
return result;
}
#endregion #region 添加一条记录
protected void addInfor(string name, string sex, string tel, string address, string pass)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./xmlPerson.xml")); XmlElement ele = doc.CreateElement("person");
ele.SetAttribute("name", name);
ele.SetAttribute("sex", sex); XmlElement ele1 = doc.CreateElement("tel");
ele1.InnerText = tel;
ele.AppendChild(ele1); XmlElement ele2 = doc.CreateElement("address");
ele2.InnerText = address;
ele.AppendChild(ele2); XmlElement ele3 = doc.CreateElement("pass");
ele3.InnerText = pass;
ele.AppendChild(ele3); XmlNode rootEle = doc.SelectSingleNode("users");
rootEle.AppendChild(ele); doc.Save(Server.MapPath("./xmlPerson.xml"));
Response.Write("添加成功!");
}
#endregion #region 删除一条记录
protected void delInfor(string name)
{
bool flag = false;
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./xmlPerson.xml")); XmlNode rootNode = doc.SelectSingleNode("users");
if (rootNode != null)
{
XmlNodeList nodes = rootNode.ChildNodes;
foreach (XmlNode node in nodes)
{
if (node.Attributes["name"].Value == name)
{
//删除的是所有的子节点
//node.RemoveAll(); rootNode.RemoveChild(node);
flag = true;
break;
}
}
} if (flag)
{
doc.Save(Server.MapPath("./xmlPerson.xml"));
Response.Write("删除成功!");
}
else
{
Response.Write("对不起,未找到此节点");
}
}
#endregion #region 修改成功
protected void editInfor(string name, string newName, string address)
{
bool flag = false;
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./xmlPerson.xml")); XmlNode rootNode = doc.SelectSingleNode("users");
if (rootNode != null)
{
foreach (XmlNode node in rootNode.ChildNodes)
{
if (node.Attributes["name"].Value == name)
{
node.Attributes["name"].Value = newName;
node.ChildNodes[1].InnerText = address;
flag = true;
break;
}
}
} if (flag)
{
doc.Save(Server.MapPath("./xmlPerson.xml"));
Response.Write("修改成功!");
}
else
{
Response.Write("对不起,未找到此节点");
}
}
#endregion