资源描述:
《一个完整 linq to xml 创建,增加,修改,删除 节点 实例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、一个完整的linqtoxml创建,增加,修改,删除节点的实例usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Xml.Linq;//xml需要用到此空间usingSystem.IO;//输入输出需要用到此空间publicpartialclassxml:System.Web.UI.Page{privatevoidcrea
2、te()//创建一个create方法{XElementxx=newXElement("学校",newXAttribute("校名","河南商专"),newXElement("系别",newXAttribute("名称","会计系"),newXElement("班级","会电17班"),newXElement("班级","会电16班"),newXElement("班级","会电15班")),newXElement("系别",newXAttribute("名称","电脑系"),newXElement("班级","电脑1班"),newXElemen
3、t("班级","电脑2班"),newXElement("班级","电脑3班")));xx.Save(Server.MapPath("xuexiao.xml"));}protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton1_Click(objectsender,EventArgse)//调用create方法创建xuexiao.xml文档{if(!File.Exists(Server.MapPath("xuexiao.xml"))){create();if(File
4、.Exists(Server.MapPath("xuexiao.xml"))){Response.Write("xuexiao.xml文档创建成功");}}else{Response.Write("xuexiao.xml文档已经存在无需再次创建");}}protectedvoidButton2_Click(objectsender,EventArgse)//遍历文档所有数据{XElementex=XElement.Load(Server.MapPath("xuexiao.xml"));varb=fromfinex.Descendants("系
5、别")selectf;foreach(variinb){Response.Write(i.ToString()+"");}}protectedvoidButton3_Click(objectsender,EventArgse){XElementex=XElement.Load(Server.MapPath("xuexiao.xml"));//带有条件的遍历varb=fromfinex.Descendants("系别")where(string)f.Attribute("名称")=="会计系"selectf;foreach(variinb){R
6、esponse.Write(i.ToString()+"");}}protectedvoidButton4_Click(objectsender,EventArgse)//修改节点{XElementex=XElement.Load(Server.MapPath("xuexiao.xml"));varb=fromfinex.Descendants("系别")where(string)f.Attribute("名称")=="会计学院"selectf;foreach(variinb){i.Attribute("名称").SetValue("会计系"
7、);}ex.Save(Server.MapPath("xuexiao.xml"));}protectedvoidButton5_Click(objectsender,EventArgse)//增加一个节点{XElementex=XElement.Load(Server.MapPath("xuexiao.xml"));XElementa=newXElement("系别",newXAttribute("名称","商务系"),newXElement("班级","商务1班"),newXElement("班级","商务2班"),newXElement(
8、"班级","商务3班"));ex.Add(a);ex.Save(Server.MapPath("xuexiao.xml"));}protectedvoidButto