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. React 与 Redux 在生产环境中的实践总结

    React 与 Redux 在生产环境中的实践总结 前段时间使用 React 与 Redux 重构了我们360netlab 的 开放数据平台.现将其中一些技术实践经验总结如下: Universal 渲 ...

  2. (3)python tkinter-消息框、对话框、文件对话框

    消息框 导入 import tkinter import tkinter.messagebox #这个是消息框,对话框的关键 提示消息框 tkinter.messagebox.showinfo('提示 ...

  3. 004.LVM缩减

    一 缩减步骤 卸载挂载点 检查文件系统 调整分区大小 缩减LV大小 重新挂载并检查 注意: 1 减少文件的大小一定需要按照上面提高的4个规定动作顺序来做,在缩减LV大小前,首先要缩减filesyste ...

  4. 006.FTP用户访问控制配置

    一 FTP控制文件 1.1 文件说明 /etc/vsftpd/ftpusers:黑名单,优先级高 #通常不修改此文件 /etc/vsftpd/user_list:黑名单,优先级相对低 注意:Linux ...

  5. [转]我的数据结构不可能这么可爱!——珂朵莉树(ODT)详解

    参考资料: Chtholly Tree (珂朵莉树) (应某毒瘤要求,删除链接,需要者自行去Bilibili搜索) 毒瘤数据结构之珂朵莉树 在全是珂学家的珂谷,你却不知道珂朵莉树?来跟诗乃一起学习珂朵 ...

  6. gdb 调试及优化

    调试程序时,在gdb内p var,会提示 No symbol "var" in current context. 即使没有使用任何编译优化选项,仍然不能查看,可能是这些变量被优化到 ...

  7. Codeforces Round #369 (Div. 2) E. ZS and The Birthday Paradox 数学

    E. ZS and The Birthday Paradox 题目连接: http://www.codeforces.com/contest/711/problem/E Description ZS ...

  8. Mac应用

    App Store 安装: AnappyApp:   截图软件 Snap:  Dock快捷键启动 izip Unarchiver: rar解压 Dr.Cleaner:内存清理.资源监控 下载安装: C ...

  9. 玩转ptrace(转)

    下面是转帖的内容,写的很详细.但是不同的linux发行版中头文件的路径和名称并不相同.如在某些发行版中<linux/user.h>就不存在,其中定义的变量出现在<asm/ptrace ...

  10. 异步接收MSMQ消息

    在这部分,我们将使用ThreadPool 和MSMQ 进行消息收发.MSMQ 是一个分布式队列,通过MSMQ 一个应用程序可以异步地与另外一个应用程序通信. 在一个典型的场景中,我们要向维护一个队列的 ...