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", ""); //创建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("");
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", ""); //name节点
XElement name = new XElement("name", "test");
//age节点
XElement age = new XElement("age", "");
//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 = ; i <= ;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(,));
//sex
XElement sex = new XElement("sex", "男女"[r.Next()]); 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 >= && age <= && 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) ==
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", ""); XElement xName = new XElement("name","张学友"); XElement xSex = new XElement("sex","男"); XElement xAge = new XElement("age",""); 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) ==
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文件操作的更多相关文章
- Android SQLite(1)简单示例-增,删,改,查
1.主要核心类,Sqlite编程要继承SQLiteOpenHelper import android.content.Context; import android.database.sqlite.S ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- 简单的php数据库操作类代码(增,删,改,查)
这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...
- python基础中的四大天王-增-删-改-查
列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...
- MongoDB增 删 改 查
增 增加单篇文档 > db.stu.insert({sn:'001', name:'lisi'}) WriteResult({ "nInserted" : 1 }) > ...
随机推荐
- MSSQL约束【转】
为了减少数据冗余和使数据库内容变的严谨,MSSQL数据库里引入了关系和约束.我们平时做一些小程序,需要使用到MSSQL数据库的时候大多没有严格去规划一下数据库的设计,但是真正开发的时候需要你严格的进行 ...
- Django-website 程序案例系列-10 cookie 和 session的应用
cookie: 现在所有网站基本都要开启cookie 客户端浏览器上的一个文件 例如: {‘key’: 'sefwefqefwefw'} 是一个键值对 简单实现cookie认证: user_in ...
- BZOJ4832[Lydsy1704月赛]抵制克苏恩——期望DP
题目描述 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q 同学会告诉你所有相关的细节.炉石传说是这样的一个游戏,每个玩家拥有一个 30 点血量的 ...
- 【Linux】Centos7 解压zip文件
如果输入unzip无反应那么请安装相应软件包 yum install -y unzip 语法 unzip [参数] [文件] 参数 -c:将解压缩的结果显示到屏幕上,并对字符做适当的转换: -f:更新 ...
- python 解释器交互模块 -- sys
sys模块是与python解释器交互的一个接口 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.getdefaultencoding(): 获取系统当前编码,一般默认为asci ...
- Codeforces Round #382 (Div. 2) C. Tennis Championship
C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- day9-13 linux基础
有道云笔记链接 http://note.youdao.com/noteshare?id=207be3d6bd79e9ff2e30b160bca1fd87
- cygwin jdk11u
cygwin jdk11u 安装 Cygwin64 下载地址 https://cygwin.com/setup-x86_64.exe Cygwin 国内源 中科大镜像源 http://mirro ...
- MT【191】阿波罗尼乌斯圆
已知$f(x)=2\sqrt{(\cos x+\frac{1}{2})^2+\sin^2 x}-\sqrt{\cos^2 x+(\sin x-\frac{1}{2})^2}$,若$m\ge f(x)$ ...
- ECMAScript 6 -- let和const命令
ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. for (let i = 0; i ; i++) {console.log(i);} ...