我要个在HTML或ASP上面对XML文件进行增删改查操作例子;
要两层的
<?xml version="1.0" encoding="utf-8" ?>
<Menus>
<Menu ParentID="0">
<id>1</id>
<title>用户管理</title>
<url></url>
</Menu>
<Menu ParentID="1">
<id>2</id>
<title>用户列表</title>
<url>admin/users.aspx</url>
</Menu>
<Menu ParentID="1">
<id>3</id>
<title>角色列表</title>
<url>admin/roles.aspx</url>
</Menu>
<Menu ParentID="0">
<id>4</id>
<title>文章管理</title>
<url>admin/articles.aspx</url>
</Menu>
<Menu ParentID="4">
<id>4</id>
<title>文章分类</title>
<url>admin/articles.aspx</url>
</Menu>
</Menus>
2008-9-1 10:11 最佳答案 http://www.codejia.com/asp/code_2005828164843_2168.htm
使用C#对XML进行增删改查操作 .
分类: C# 2009-10-17 00:19 2049人阅读 评论(5) 收藏 举报
xml文件格式
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<messageList>
03. <message id="1">
04. <Name>熊猫</Name>
05. <QQ>123456</QQ>
06. <Email>panda@qq.com</Email>
07. <Content>asfdsafasdfasdfsad我是内容</Content>
08. <Time>2009-10-1</Time>
09. </message>
10.</messageList>
<?xml version="1.0" encoding="utf-8"?>
<messageList>
<message id="1">
<Name>熊猫</Name>
<QQ>123456</QQ>
<Email>panda@qq.com</Email>
<Content>asfdsafasdfasdfsad我是内容</Content>
<Time>2009-10-1</Time>
</message>
</messageList>
增加>>>>
view plaincopy to clipboardprint?
01.XmlDocument xmld = new XmlDocument();
02. xmld.Load(Server.MapPath("App_Code/Message.xml"));//加载xml文件
03. XmlNode root = xmld.SelectSingleNode("messageList");//查找messageList节点
04.
05. int i = xmld.SelectSingleNode("messageList").ChildNodes.Count + 1;
06. XmlElement xml0 = xmld.CreateElement("message");
07. xml0.SetAttribute("id", i.ToString());
08.
09. XmlElement xml1 = xmld.CreateElement("Name");//创建一个Name节点
10. xml1.InnerText = this.txtName.Value.Trim();
11. xml0.AppendChild(xml1);//添加
12.
13. XmlElement xml2 = xmld.CreateElement("QQ");//创建QQ节点
14. xml2.InnerText = this.txtQQ.Value.Trim();
15. xml0.AppendChild(xml2);//添加
16.
17. XmlElement xml3 = xmld.CreateElement("Email");//创建一个Email节点
18. xml3.InnerText = this.txtEmail.Value.Trim();
19. xml0.AppendChild(xml3);//添加
20.
21. XmlElement xml4 = xmld.CreateElement("Content");//创建一个Content节点
22. xml4.InnerText = this.txtContent.Value;
23. xml0.AppendChild(xml4);//添加
24.
25. XmlElement xml5 = xmld.CreateElement("Time");//创建一个Time节点
26. xml5.InnerText = DateTime.Now.ToString("yyyy-MM-dd");
27. xml0.AppendChild(xml5);//添加
28.
29. root.AppendChild(xml0);
30.
31. xmld.Save(Server.MapPath("App_Code/Message.xml"));
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));//加载xml文件
XmlNode root = xmld.SelectSingleNode("messageList");//查找messageList节点
int i = xmld.SelectSingleNode("messageList").ChildNodes.Count + 1;
XmlElement xml0 = xmld.CreateElement("message");
xml0.SetAttribute("id", i.ToString());
XmlElement xml1 = xmld.CreateElement("Name");//创建一个Name节点
xml1.InnerText = this.txtName.Value.Trim();
xml0.AppendChild(xml1);//添加
XmlElement xml2 = xmld.CreateElement("QQ");//创建QQ节点
xml2.InnerText = this.txtQQ.Value.Trim();
xml0.AppendChild(xml2);//添加
XmlElement xml3 = xmld.CreateElement("Email");//创建一个Email节点
xml3.InnerText = this.txtEmail.Value.Trim();
xml0.AppendChild(xml3);//添加
XmlElement xml4 = xmld.CreateElement("Content");//创建一个Content节点
xml4.InnerText = this.txtContent.Value;
xml0.AppendChild(xml4);//添加
XmlElement xml5 = xmld.CreateElement("Time");//创建一个Time节点
xml5.InnerText = DateTime.Now.ToString("yyyy-MM-dd");
xml0.AppendChild(xml5);//添加
root.AppendChild(xml0);
xmld.Save(Server.MapPath("App_Code/Message.xml"));
删除>>>>
view plaincopy to clipboardprint?
01.XmlDocument xmld = new XmlDocument();
02.
03. xmld.Load(Server.MapPath("App_Code/Message.xml"));
04.
05. XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
06.
07. foreach (XmlNode var in list)
08. {
09. XmlElement xmle = (XmlElement)var;
10.
11. if (xmle.GetAttribute("id") == Id)//如果ID等于文本的值就删除
12. {
13. xmle.RemoveAll();
14. xmld.Save(Server.MapPath("App_Code/Message.xml"));//删除后记得一定要保存
15. Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除成功!!');", true);
16. }
17. else
18. {
19. Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除失败!!');", true);
20. }
21. }
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
foreach (XmlNode var in list)
{
XmlElement xmle = (XmlElement)var;
if (xmle.GetAttribute("id") == Id)//如果ID等于文本的值就删除
{
xmle.RemoveAll();
xmld.Save(Server.MapPath("App_Code/Message.xml"));//删除后记得一定要保存
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除成功!!');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除失败!!');", true);
}
}
更改>>>>
view plaincopy to clipboardprint?
01.XmlDocument xmld = new XmlDocument();
02. xmld.Load(Server.MapPath("App_Code/Message.xml"));
03.
04. XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
05.
06. foreach (XmlNode xmln in list)
07. {
08. XmlElement xmle = (XmlElement)xmln;//格式转换
09. if (Request["ID"].Equals(xmle.GetAttribute("id")))
10. {
11. xmle.ChildNodes[0].InnerText = this.txtName.Value;
12. xmle.ChildNodes[1].InnerText = this.txtQQ.Value;
13. xmle.ChildNodes[2].InnerText = this.txtEmail.Value;
14. xmle.ChildNodes[3].InnerText = this.txtContent.Value;
15.
16. xmld.Save(Server.MapPath("App_Code/Message.xml"));
17. Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功!!!');location.href='MessageList.aspx'",true);
18. }
19. }
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
foreach (XmlNode xmln in list)
{
XmlElement xmle = (XmlElement)xmln;//格式转换
if (Request["ID"].Equals(xmle.GetAttribute("id")))
{
xmle.ChildNodes[0].InnerText = this.txtName.Value;
xmle.ChildNodes[1].InnerText = this.txtQQ.Value;
- 1
- 2
- 3
- 4
- 5
前往页