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 }) > ...
随机推荐
- Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 6. 条件
前面的教程中,我们已经可以让小海龟绘制出各种图形了.但是,所有绘图的代码都是预先编好的,程序一旦运行起来,运行结果(绘制的图形)就是固定不变的.这一节中,咪博士将教大家如何让海龟响应用户的输入. im ...
- delphi7调用数据库连接属性
背景:连接数据库用ADOQuery控件,但是程序一旦编译完成,如果想更改数据库连接设置还得重新修改ADOQuery的属性重新编译 如果可以在程序中可以随时设置ADOQuery的属性则会方便很多. 实现 ...
- 【Linux】工作管理
在进行工作管理的行为中,其实每个工作都是目前bash的子进程,即彼此间是有相关性的.我们无法以job control的方式由tty1的环境去管理tty2的bash 当只有一个终端时,可以出现提示符让你 ...
- c-lodop云打印实现手机打印 JS语句打印
Lodop和c-lodop目前只能安装到windows操作系统上,但是其他操作系统可通过向C-Lodop安装的电脑发送打印任务,实现手机广域网或局域网打印,打印语句也是简单的JS语句,可以轻松实现云打 ...
- 如何停止处于stopping状态的windows服务
工作中有时需要启动和停止windows service,有时候会出现服务处于stopping或者starting的状态,但是,在services界面中,start service/stop servi ...
- KEYENCE Programming Contest 2019 自闭记
A:签到. #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> ...
- Web Scraper爬取就是这么简单
这应该是最全的一个文档了 https://www.jianshu.com/p/e4c1561a3ea7 所以我就不介绍了,大家直接看就可以了,有问题可以提出来,我会针对问题对文章进行补充~
- Leetcode 7.反转整数 By Python
给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...
- 自学Zabbix3.12.3-动作Action-自动发现action配置
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.12.3-动作Action-自动发现action配置 1. 首先先学习 Ne ...
- for循环实例
for循环“池理解”[root@localhost ~]# vi showday.sh#!/bin/bashfor TM in "Morning" "Noon" ...