LINQ to XML简介
我们的配置文件使用XML存储信息。ADO.NET的DataSet(利用扩展方法)可以方便的将数据保存(或加载)为XML。.NET特有的XML API,如XmlReader/XmlWriter类。微端提供了一个特殊的程序集 System.Xml.dll专门对XML文档进行编程。因为LINQ操作数据比较方便,所有大多数人选择使用Linq to XML API来于XML数据进行交互。
传统的XML写法
这个用法就跟用JavaScript在页面上创建元素一样的
//在内存中新建一个Xml文档
XmlDocument doc = new XmlDocument(); //穿件元素 <inventory>
XmlElement inventory = doc.CreateElement("Inventory"); //创建<Car>子元素 包含一个ID特性
XmlElement car = doc.CreateElement("Car");
car.SetAttribute("ID", ""); //创建Car元素中的数据
XmlElement name = doc.CreateElement("PetName");
name.InnerText = "Jimbo";
XmlElement color = doc.CreateElement("Color");
color.InnerText = "Red";
XmlElement make = doc.CreateElement("Make");
make.InnerText = "Ford"; //将<PetName><Color><Make>
car.AppendChild(name);
car.AppendChild(color);
car.AppendChild(make); //将<Car>元素添加到<Inventory>元素
inventory.AppendChild(car); //将完整的XML插入到XmlDocument对象并保存文件
doc.AppendChild(inventory);
doc.Save("Inventory.xml");

更优秀的Dom———Linq to XML
System.XML.Linq包含了一组非常易于管理的类,它们代表一个XML文档的不同方面(元素、属性、XML命名控件、XML注释、处理指令等)
//注意把格式调一下,可以看的很清除
XElement doc = new XElement("Inventory",
new XElement("Car", new XAttribute("ID", ""),
new XElement("PetName", "Jimbo"),
new XElement("Color", "Red"),
new XElement("Color", "Ford")
)
);

这样对比一下,下面的一种更简些。
System.Xml.Linq 命名空间的成员
LinQ to XML的核心程序集(System.Xml.Linq.dll)只在三个不同空间下定义了很少的类型。这 三个命名空间是 System.Xml.Linq、System.Xml.Schema、System.Xml.xPath.
System.Xml.Linq命名空间的成员




LINQ to XML的轴方法
除这些X*类,System.Xml.Linq中还定义了一个名为Extensions的类,它还定义了一组针对IEnumerable<T>的扩展方法,其中为XNode或XContainer的子类。
LINQ to XML的Extensions类的Selecr成员







这些方法允许在一个已加载的XML树中进行查询。以查找元素、特性以及他们的值。这么方法被称为轴方法(axis method)或简称轴。抽象类XContainer类支持很多于Extensions方法的名称相同的方法。由于XContainer是XElement和XDocument的父类,所以都支持全部的功能。
例如:
XElement doc = new XElement("Inventory",
new XElement("Car", new XAttribute("ID", ""),
new XElement("PetName", "Jimbo"),
new XElement("Color", "Red"),
new XElement("Color", "Ford")
)
);
doc.Descendants("PetName").Remove(); //先找到该元素再删除,Linq一样
doc.Save("InventoryWithLinq1.xml");

奇妙的XName和XNamespace
Desendants<XName name>

相反,此类提供了隐式转换 String ,允许您创建 XName.他会将一个简单的System.String映射到正确的XName对象。XNamespace一样。
使用XElement和XDocument
XDocument表示整个XML文档。它可以用来定义一个根元素及其包含的所有元素、处理指令和XML声明。
XDocument inventroyDoc = new XDocument(
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Current Inventroy of cars"),
new XProcessingInstruction("xml-stylesheet", "href='MyStyless.css' title='Compact' type='text/css'"),
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", ""),
new XElement("Color", "Green"),
new XElement("Make", "BWM"),
new XElement("PetName", "stan")
),
new XElement("Car", new XAttribute("ID", ""),
new XElement("Color", "Pink"),
new XElement("Make", "Yugo"),
new XElement("PetName", "Melvin")
) )
));
inventroyDoc.Save("test.xml");

可以简单的就用XElement来创建XML元素。
从数组和容器中生成文档
读取数据身材XElement(或XDocument).
//创建一个匿名类型的数组
var people = new[]{
new {FirstName="Mandy",Age=},
new {FirstName="Andrew",Age=},
new {FirstName="Dave",Age=},
new {FirstName="Sara",Age=},
}; XElement peopleDoc = new XElement("Perple", from c in people select new XElement("Person", new XAttribute("Age", c.Age), new XElement("FirstName", c.FirstName))
); //或者 都会得到相同的结果
var arrayDataAdsXlements = from c in people select new XElement("Person", new XAttribute("Age", c.Age), new XElement("FirstName", c.FirstName));
XElement peopleDoc2 = new XElement("People", arrayDataAdsXlements); peopleDoc.Save("one.xml");
peopleDoc2.Save("two.xml");

加载和解析XML内容
XElement和XDocument都支持Load()和Parse()方法,可以从包含XML数据的string对象或外部XML文件中获取XML对象模型。
string myElement =
@"<Car ID='3'>
<Color>Yellow</Color>
<Make>Yugo</Make>
</Car>";
XElement newElement = XElement.Parse(myElement);
Console.WriteLine(newElement); Console.WriteLine();
XDocument myDoc = XDocument.Load("one.xml");
Console.WriteLine(myDoc);

定义LINQ to XML辅助类
public static void InsertNewElement(string make, string color, string petName) //一个添加的方法
{
XDocument inventoryDoc = XDocument.Load("test.xml");
Random r = new Random();
XElement newElement = new XElement("Car",
new XAttribute("ID", r.Next()),
new XElement("Color", color),
new XElement("Make", make),
new XElement("PetName", petName)); inventoryDoc.Descendants("Inventory").First().Add(newElement);
inventoryDoc.Save("test.xml");
}
原:

运行该方法:


查询:

LINQ to XML简介的更多相关文章
- [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界
本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...
- LINQ系列:LINQ to XML类
LINQ to XML由System.Xml.Linq namespace实现,该namespace包含处理XML时用到的所有类.在使用LINQ to XML时需要添加System.Xml.Linq. ...
- LINQ系列:LINQ to XML操作
LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...
- LINQ系列:LINQ to XML查询
1. 读取XML文件 XDocument和XElement类都提供了导入XML文件的Load()方法,可以读取XML文件的内容,并转换为XDocument或XElement类的实例. 示例XML文件: ...
- Linq to Xml读取复杂xml(带命名空间)
前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...
- c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)
主界面
- Linq对XML的简单操作
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- LINQ to XML 编程基础
1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: 隐藏行号 复制代码 ?创建 XML public static void CreateDocumen ...
- XML基础学习02<linq to xml>
Linq to XML的理解 1:这是一种比较好的操作Xml的工具. àXDocument 文档 àXElement 元素 àXAttribute 属性 àXText 文本 2:这里还是和我们之前创建 ...
随机推荐
- 关于日常使用Azure MySQL中遇到的连接问题以及排查方法分享
由于防火墙问题,TCP keep alive 问题,以及 MySQL 自身的参数问题这三个在使用中比较常见,所以今天就分享下自己找到的排查方法. 今天先聊一聊防火墙问题 大多数人在第一次创建 MySQ ...
- C#自定义控件 在 Toolbox显示不了的问题
1) Close your solution2) Tools->Options->"Windows Form Designer" - find AutoToolboxP ...
- 错误 6 未能找到类型或命名空间名称“BLL”(是否缺少 using 指令或程序集引用?)
出现这个错误,要确保先引用BLL.dll, 如果引用之后还没有解决这个问题的话,确认一下程序集FrameWork版本与项目FrameWork版本一致.右击程序集属性可以更改.
- (0!=0)==true? 记一个匪夷所思的问题
最近换了份工作,公司的开发框架是基于SSH自己搭建的.这个问题是我在解决一个需求的时候遇到的,其实解决这个疑惑的过程也就是读框架源码的过程,特此记录一下. 问题:ba.getState()!=CbBa ...
- Jmeter压力测试入门操作
Jmeter压力测试入门 1. 前言 Jmeter 是Apache组织开发的基于Java的压力测试工具,开源并且支持多个操作系统,是一款很好的HTTP测试工具.本篇文章主要的目的是帮助没有接触过J ...
- glyphicons-halflings-regular.woff2 文件 404
搜索了下,果然是因为mine没有配置的原因. http://stackoverflow.com/questions/32300578/how-to-remove-error-about-glyphic ...
- 服务器LIUNX之如何解决矿机问题
点进来的基本都是遇到liunx变矿机的小伙伴吧(cpu运载300%) 卡的连终端都很难打开 开下来之后提示 大意是, 到xxx网站给钱了事, 不过基本这个网站基本也上不去, 要么是暴力破解, 要么是通 ...
- linux定时任务crontab的使用
crond进程: crond是linux下用来周期性地执行某种任务的一个守护进程,安装操作系统默认会安装此服务工具,并且会自动启动crond进程. 设置定时任务过程: 1. 创建任务文件(.sh) [ ...
- Android滑动删除功能
今天学习了新的功能那就是滑动删除数据.先看一下效果 我想这个效果大家都很熟悉吧.是不是在qq上看见过这个效果.俗话说好记性不如赖笔头,为了我的以后,为了跟我一样自学的小伙伴们,我把我的代码粘贴在下面. ...
- HTML表单特别效果—音量调节,购物数量
<form oninput="x.value=parseInt(a.value)+parseInt(b.value)">0<input type="ra ...