Query:
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".//db//dbGuest.xml"));
//User是XML根节点,Name字节点
lbEmail.Text = doc.SelectSingleNode("//User[Name='"+ddlName.SelectedItem.Text+"']").ChildNodes.Item(2).InnerText;
Delete:
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)
{
if(node[0].InnerText==ddlName.SelectedItem.Text)
xe.RemoveAll();//删除该节点的全部内容
break;
}
}
xmlDoc.Save(Server.MapPath(".//db//dbGuest.xml"));
Add:
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"));
Change:
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"));
ds.ReadXml(Server.MapPath(@"./db/dbGuest.xml"));
//以下为添加记录
DataRow dr = ds.Tables[0].NewRow();
dr["Name"] = "tian";
dr["City"] = "ocean";
dr["Email"] = "22@163.com "; dr["Message"] = "9999"; dr["STime"] = DateTime.Now.ToString(); ds.Tables[0].Rows.Add(dr); // 写回XML ds.WriteXml(Server.MapPath(".//db//dbGuest.xml"));