LINQ to XML的出现使得我们再也不需要使用XMLDocument这样复杂的一个个的没有层次感的添加和删除。LINQ可以使的生成的XML文档在内存中错落有致。下面以一个小的例子说名LINQ to XML的简单应用。

  • 需要添加必要的引用。System.XML.Linq , System.XML.Xpath
  • 使用XDocument 建立一个XML文档。XDeclaration 声明开头字样。XComment 添加相应的注释。而XElement则是NODE的名字和内容。
  • 把一个XDocument 存储起来使用Save方法,而读取是Load,参数可以指定所要的路径。
  • 增加 使用Add()方法,新建节点。或者使用LINQ语句配合使用。
  • 读取,可以通过XElement对象的Descendant()的名字,读取相应的node,Value属性是node对应的值,而Name是node的名字。

Code
var userName = from un in xe.Descendants("Name")
select un.Value;
  • 修改可以 使用2种方法。
  1. 其一是直接对Value和Name进行数值的修改。
  2. 使用Replace相关的方法等等。比如

Code
var a = age.Single<XElement>();
a.ReplaceWith(new XElement("agettt", ""));
//读取唯一一个,进行名字和数字的修改。
  • 删除一个或者多个node,或者单独删除里面的值而不删除节点名字。可以有多种方法,Remove ,Removeall,RemoveNodes等等方法。

下面是一个完整的控制台程序,以演示简单的增删改功能的实现。是不是比XMLDocument来的简单得多呢,大家赶快试试吧:)

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;
using System.Xml.XPath; namespace LINQTOXMLDEMO
{
class Program
{
staticvoid Main(string[] args)
{
XDocument userInfomation =new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("This is a comment.Just input what you want to say."),
new XElement("UsersInfomation",
new XElement("User", new XAttribute("ID", ""),
new XElement("Name", "Alex"),
new XElement("Hometown", "HRB"),
new XElement("Age", "")),
new XElement("User",
new XAttribute("ID", ""),
new XElement("Name", "Fu"),
new XElement("Hometown", "SY"),
new XElement("Age", "")),
new XElement("User",new XAttribute("ID",""),
new XElement("Name","Abe"),
new XElement("Hometown","HRB"),
new XElement("Age",""))
)); // 显示所生成的XML在内存中的信息。并且按着所制定的路径保存。
Console.WriteLine(userInfomation);
userInfomation.Save(@"D:\1\userInfomation.xml"); XElement xd = XElement.Load(@"D:\1\userInfomation.xml");
GetAllUsername(xd);
Console.WriteLine();
GetAge(xd);
Console.WriteLine();
GetHometown(xd);
Console.WriteLine();
updateAge(xd);
//Console.WriteLine(xd);
deleteElement(xd);
Console.WriteLine();
addNode(xd);
Console.WriteLine(xd);
Console.ReadKey();
}
//得到所有name字段的名字
privatestaticvoid GetAllUsername(XElement xe)
{
var userName = from un in xe.Descendants("Name")
select un.Value;
foreach (var name in userName)
{
Console.WriteLine("name {0}" ,name);
}
}
//输出AGE是24的所有NODE
privatestaticvoid GetAge(XElement xe)
{
var age = from c in xe.Descendants("Age")
where c.Value ==""
select c;
foreach (var a in age)
Console.WriteLine("AGE: {0}", a);
}
//移除所有的Name字段
privatestaticvoid deleteElement(XElement xe)
{
var user = from u in xe.Descendants("User")
select u;
foreach (var u in user)
{ var names = from name in u.Descendants("Name")
select name;
names.Remove();
}
}
//增加节点
privatestaticvoid addNode(XElement xe)
{
var user = from u in xe.Descendants("User")
select u;
foreach (var u in user)
{
u.Add(new XElement("Nation","China"));
}
}
//更新字段NODE的名字和内容。
privatestaticvoid updateAge(XElement xe)
{
var age = from c in xe.Descendants("Age")
where c.Value ==""
select c;
var a = age.Single<XElement>();
a.ReplaceWith(new XElement("agettt", ""));
}
//统计hometown为HRB的人数
privatestaticvoid GetHometown(XElement xe)
{
var hometown = from h in xe.Descendants("Hometown")
where h.Value =="HRB"
select h;
int count=;
foreach (var h in hometown)
{
count++;
}
Console.WriteLine(count);
}
}
}

几个例子的作用,代码函数都有注释。比较简单。不需要太多的介绍。输出结果如下图所示。

 
 
 

LINQ to XML 建立,读取,增,删,改的更多相关文章

  1. 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据

    第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...

  2. C# ADO.NET (sql语句连接方式)(增,删,改)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  4. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  5. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  6. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  7. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  8. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  9. StringBuilder修改字符串内容,增,删,改,插

    package seday01;/** * 字符串不变对象特性只针对字符串重用,并没有考虑修改操作的性能.因此String不适合频繁修改内容. * 若有频繁修改操作,使用StringBuilder来完 ...

随机推荐

  1. linux(centos)安装升级ruby

    参考文章:http://www.ruby-lang.org/zh_cn/documentation/installation/ 文章给出了不同平台的多种方法.我的是centos,我选择了一个比较简单的 ...

  2. 【noip模拟赛1】古韵之刺绣

    描述 日暮堂前花蕊娇, 争拈小笔上床描, 绣成安向春园里, 引得黄莺下柳条. ——胡令能<咏绣障> 古时女子四德中有一项——女红.女红的精巧程度对于女子来说是十分重要的.韵哲君十分爱好女红 ...

  3. 迈出第一步,Hexo博客搭建

    很早之前看到别人的博客就总想着自己之后也要搭一个,最近突然来了干劲,就开始搭起了博客.不过搭博客还真是一个累活,失败了不下十次,用了好几天的时间,感觉自己在浪费时间,但是看到现在博客终于能用了,非常开 ...

  4. 一个轻巧高效的多线程c++stream风格异步日志(二)

    目录 一个轻巧高效的多线程c++stream风格异步日志(二) 前言 LogFile类 AsyncLogging类 AsyncLogging实现 增加备用缓存 结语 一个轻巧高效的多线程c++stre ...

  5. python 字典相关操作

    字典 字典的增删改查 字典的创建方式: # 创建字典类型 info = { 'name':'李白', 'age':'25', 'sex':'男' } msg = { 'user01':'Longzel ...

  6. HDU 1402 A * B Problem Plus 快速傅里叶变换 FFT 多项式

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 快速傅里叶变换优化的高精度乘法. https://blog.csdn.net/ggn_2015/artic ...

  7. BZOJ.2462.[BeiJing2011]矩阵模板(二维Hash)

    题目链接 序列上的Hash和前缀和差不多,二维Hash也和二维前缀和差不多了. 预处理大矩阵所有r*c的小矩阵hash值,再对询问的矩阵Hash. 类比于序列上\(s[r]-s[l-1]*pow[r- ...

  8. 2018年牛客网NOIP赛前训练营游记

    2018年牛客网NOIP赛前训练营游记 提高组(第一场) 中位数 #include<cstdio> #include<cctype> #include<climits&g ...

  9. request.getRequestDispatcher("").forward()中文乱码

    即使jsp页面的编码已设为“UTF-8”,有中文的地方还是会出现乱码,但是用response.sendRedirect不会出现此问题. 解决方案一: 不使用PrintWriter out=respon ...

  10. 利用dynamic简化数据库的访问

    今天写了一个数据库的帮助类,代码如下. public static class DbEx { public static dynamic ReadToObject(this IDataReader r ...