首页 > 代码库 > linq to xml 简单的增、删、改、查、保存xml文件操作
linq to xml 简单的增、删、改、查、保存xml文件操作
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.IO;using System.Xml;namespace test{ public partial class xmlDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //使用XmlDocument创建xml文件 protected void Button1_Click(object sender, EventArgs e) { string dirPath = Server.MapPath("/xml"); DirectoryInfo dir = new DirectoryInfo(dirPath); if (!dir.Exists) { dir.Create(); } //创建xml文件 XmlDocument xdoc = new XmlDocument(); //创建xml描述 XmlDeclaration xdec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null); xdoc.AppendChild(xdec); //创建xml跟节点 XmlElement xroot = xdoc.CreateElement("persons"); xdoc.AppendChild(xroot); //创建节点 XmlElement xper = xdoc.CreateElement("person"); xroot.AppendChild(xper); //对person节点设置属性 xper.SetAttribute("id", "0001"); //创建person节点下的子节点 //person节点下创建name节点 XmlElement xname = xdoc.CreateElement("name"); xper.AppendChild(xname); XmlText textName = xdoc.CreateTextNode("test"); xname.AppendChild(textName); //person节点下创建age节点 XmlElement xage = xdoc.CreateElement("age"); xper.AppendChild(xage); XmlText textAge = xdoc.CreateTextNode("20"); xage.AppendChild(textAge); //person节点下创建sex节点 XmlElement xsex = xdoc.CreateElement("sex"); xper.AppendChild(xsex); XmlText textsex = xdoc.CreateTextNode("男"); xsex.AppendChild(textsex); //保存xml文件 xdoc.Save(dirPath+"/"+"person.xml"); Response.Write("创建xml文件成功"); } //使用linq to xml创建xml文件 protected void Button2_Click(object sender, EventArgs e) { string dirPath = Server.MapPath("/xml"); DirectoryInfo dir = new DirectoryInfo(dirPath); if (!dir.Exists) { dir.Create(); } XDocument xdoc = new XDocument(); //使用XDocument 可以不需要document描述 //创建跟节点 XElement root = new XElement("root"); //添加跟节点 xdoc.Add(root); //创建person节点 XElement per = new XElement("person"); //添加person节点 root.Add(per); //创建属性节点 XAttribute xId = new XAttribute("id", "0002222"); //name节点 XElement name = new XElement("name", "test"); //age节点 XElement age = new XElement("age", "22"); //sex节点 XElement sex = new XElement("sex", "男"); //对person节点添加id属性 以及name sex age节点 per.Add(xId,name, sex, age); //保存linq to xml 文件 xdoc.Save(dirPath + "/" + "linqToXml.xml"); Response.Write("linq to xml保存文件成功"); } //使用linq to xml 批量添加person节点 //xDocument.Root.Add() 批量添加 protected void Button3_Click(object sender, EventArgs e) { string dirPath = Server.MapPath("/xml"); DirectoryInfo dir = new DirectoryInfo(dirPath); if (!dir.Exists) { dir.Create(); } XDocument xdoc = new XDocument(); //使用XDocument 可以不需要document描述 //创建跟节点 XElement root = new XElement("root"); xdoc.Add(root); Random r = new Random(); for (int i = 1; i <= 100;i++ ) { //person节点 XElement per = new XElement("person"); //id属性 XAttribute xId = new XAttribute("id", i); //name XElement name = new XElement("name","test"+i); //age XElement age = new XElement("age",r.Next(18,30)); //sex XElement sex = new XElement("sex", "男女"[r.Next(2)]); per.Add(xId, name, sex, age); xdoc.Root.Add(per); } //保存linq to xml 文件 xdoc.Save(dirPath + "/" + "personList.xml"); Response.Write("linq to xml批量添加成功"); } //linq to xml 查找节点 protected void Button5_Click(object sender, EventArgs e) { string path = Server.MapPath("/xml/personList.xml"); XDocument xdoc = XDocument.Load(path); var query = xdoc.DescendantNodes().Where( el => { //将el转为XElement对象 XElement xe = el as XElement; if (xe == null) { return false; } //找到age节点 XElement xAge = xe.Element("age"); //找到sex节点 XElement xSex = xe.Element("sex"); if(xAge !=null && xSex !=null) { int age = Convert.ToInt32(xAge.Value); string sex = xSex.Value; if (age >= 20 && age <= 25 && sex == "女") { return true; } } return false; } ); //将查到的节点保存到xml文件中 string savePath = Server.MapPath("/xml/search.xml"); XDocument saveXdoc = new XDocument(); XElement root = new XElement("root"); saveXdoc.Add(root); root.Add(query); saveXdoc.Save(savePath); Response.Write("linq to xml查找成功"); } //linq to xml 修改节点 protected void Button4_Click(object sender, EventArgs e) { string path = Server.MapPath("/xml/personList.xml"); XDocument xdoc = XDocument.Load(path); var query = from p in xdoc.Descendants("person") where Convert.ToInt32(p.Attribute("id").Value) ==1 select p; var first = query.FirstOrDefault(); if (first != null) { XElement searchElement = first as XElement; XElement xname = searchElement.Element("name"); xname.Value = "刘德华"; xdoc.Save(path); Response.Write("linq to xml修改成功"); } } //linq to xml 添加节点 protected void Button6_Click(object sender, EventArgs e) { string path = Server.MapPath("/xml/personList.xml"); XDocument xdoc = XDocument.Load(path); //找到根节点 XElement root = xdoc.Element("root"); XElement xPer = new XElement("person"); XAttribute xid = new XAttribute("id", "9999"); XElement xName = new XElement("name","张学友"); XElement xSex = new XElement("sex","男"); XElement xAge = new XElement("age","99"); root.Add(xPer); xPer.Add(xPer, xid, xName, xAge, xSex); xdoc.Save(path); Response.Write("添加成功"); } //linq to xml 删除节点 protected void Button7_Click(object sender, EventArgs e) { string path = Server.MapPath("/xml/personList.xml"); XDocument xdoc = XDocument.Load(path); var query = from p in xdoc.Descendants("person") where Convert.ToInt32(p.Attribute("id").Value) == 1 select p; var first = query.FirstOrDefault(); if (first != null) { XElement searchElement = first as XElement; searchElement.Remove(); xdoc.Save(path); Response.Write("linq to xml删除成功"); } } }}
linq to xml 简单的增、删、改、查、保存xml文件操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。