<?xml version="1.0" standalone="yes"?>
<dbGuest>
<User>
<Name>shaoazhd</Name>
<City>beijing</City>
<Email>sss@22.net</Email>
<Message>afsa</Message>
<STime>2004-7-12 15:07:39</STime>
</User>
<User>
<Name>Guset</Name>
<City>上海</City>
<Email>ss@22.net</Email>
<Message>afsa</Message>
<STime>2004-7-12 15:07:39</STime>
</User>
</dbGuest>
<dbGuest>
<User>
<Name>shaoazhd</Name>
<City>beijing</City>
<Email>sss@22.net</Email>
<Message>afsa</Message>
<STime>2004-7-12 15:07:39</STime>
</User>
<User>
<Name>Guset</Name>
<City>上海</City>
<Email>ss@22.net</Email>
<Message>afsa</Message>
<STime>2004-7-12 15:07:39</STime>
</User>
</dbGuest>
C#
private void btnQuery_Click(object sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
lbEmail.Text = doc.SelectSingleNode("//User[Name='" + ddlName.SelectedItem.Text + "']").ChildNodes.Item(2).InnerText;
}
private void btnChange_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("dbGuest").ChildNodes;//获取dbGuest节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
XmlNodeList node = xe.GetElementsByTagName("Name");
if (node.Count > 0)
{
if (node[0].InnerText == ddlName.SelectedItem.Text)
{
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "Email")//如果找到
{
xe2.InnerText = tbNewMail.Text;//则修改
break;//找到退出来就可以了
}
}
break;
}
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("dbGuest").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList node = xe.GetElementsByTagName("Name");
if (node.Count > 0)
{
for (int i = 0; i < node.Count; i++)
{
if (node[i].InnerText == ddlName.SelectedItem.Text)
{
xe.RemoveAll();//删除该节点的全部内容
//break;
}
}
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNode root = xmlDoc.SelectSingleNode("dbGuest");//查找<dbGuest>
XmlElement xe1 = xmlDoc.CreateElement("User");//创建一个<User>节点
XmlElement xesub1 = xmlDoc.CreateElement("Name");
xesub1.InnerText = "Guset";//设置文本节点
xe1.AppendChild(xesub1);//添加到<User>节点中
XmlElement xesub2 = xmlDoc.CreateElement("City");
xesub2.InnerText = "上海";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("Email");
xesub3.InnerText = "ss@22.net";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<dbGuest>节点中
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void Bind()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(".\\db\\dbGuest.xml"));
dgShow.DataSource = ds.Tables[0].DefaultView;
dgShow.DataBind();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList elemList = doc.GetElementsByTagName("Name");
ddlName.Items.Clear();
for (int i = 0; i < elemList.Count; i++)
ddlName.Items.Add(elemList[i].InnerXml);
}
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
lbEmail.Text = doc.SelectSingleNode("//User[Name='" + ddlName.SelectedItem.Text + "']").ChildNodes.Item(2).InnerText;
}
private void btnChange_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("dbGuest").ChildNodes;//获取dbGuest节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
XmlNodeList node = xe.GetElementsByTagName("Name");
if (node.Count > 0)
{
if (node[0].InnerText == ddlName.SelectedItem.Text)
{
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "Email")//如果找到
{
xe2.InnerText = tbNewMail.Text;//则修改
break;//找到退出来就可以了
}
}
break;
}
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("dbGuest").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList node = xe.GetElementsByTagName("Name");
if (node.Count > 0)
{
for (int i = 0; i < node.Count; i++)
{
if (node[i].InnerText == ddlName.SelectedItem.Text)
{
xe.RemoveAll();//删除该节点的全部内容
//break;
}
}
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNode root = xmlDoc.SelectSingleNode("dbGuest");//查找<dbGuest>
XmlElement xe1 = xmlDoc.CreateElement("User");//创建一个<User>节点
XmlElement xesub1 = xmlDoc.CreateElement("Name");
xesub1.InnerText = "Guset";//设置文本节点
xe1.AppendChild(xesub1);//添加到<User>节点中
XmlElement xesub2 = xmlDoc.CreateElement("City");
xesub2.InnerText = "上海";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("Email");
xesub3.InnerText = "ss@22.net";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<dbGuest>节点中
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void Bind()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(".\\db\\dbGuest.xml"));
dgShow.DataSource = ds.Tables[0].DefaultView;
dgShow.DataBind();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList elemList = doc.GetElementsByTagName("Name");
ddlName.Items.Clear();
for (int i = 0; i < elemList.Count; i++)
ddlName.Items.Add(elemList[i].InnerXml);
}