我们的配置文件使用XML存储信息。ADO.NET的DataSet(利用扩展方法)可以方便的将数据保存(或加载)为XML。.NET特有的XML API,如XmlReader/XmlWriter类。微端提供了一个特殊的程序集 System.Xml.dll专门对XML文档进行编程。因为LINQ操作数据比较方便,所有大多数人选择使用Linq to XML API来于XML数据进行交互。

传统的XML写法

XmlElement类的介绍

这个用法就跟用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简介的更多相关文章

  1. [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界

    本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...

  2. LINQ系列:LINQ to XML类

    LINQ to XML由System.Xml.Linq namespace实现,该namespace包含处理XML时用到的所有类.在使用LINQ to XML时需要添加System.Xml.Linq. ...

  3. LINQ系列:LINQ to XML操作

    LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...

  4. LINQ系列:LINQ to XML查询

    1. 读取XML文件 XDocument和XElement类都提供了导入XML文件的Load()方法,可以读取XML文件的内容,并转换为XDocument或XElement类的实例. 示例XML文件: ...

  5. Linq to Xml读取复杂xml(带命名空间)

    前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...

  6. c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)

    主界面

  7. Linq对XML的简单操作

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  8. LINQ to XML 编程基础

    1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: 隐藏行号 复制代码 ?创建 XML public static void CreateDocumen ...

  9. XML基础学习02<linq to xml>

    Linq to XML的理解 1:这是一种比较好的操作Xml的工具. àXDocument 文档 àXElement 元素 àXAttribute 属性 àXText 文本 2:这里还是和我们之前创建 ...

随机推荐

  1. java读取excel(只是读取)

      最近做项目需要读取excel,在网上找了几个,都需要下载各种jar,下载好之后还是不能用,而且还分(xls xlsx)这两种格式, 最后找到个这个,不需要下载jar包,格式通吃,不过只是简单的读取 ...

  2. maven module

    通过将一个maven项目拆分成多个module,会引入一定的项目复杂度,但随着后期项目代码的逐渐增多,最直观的感受是,每次build代码,不必build整个项目,可节省很多时间. 如果各个module ...

  3. 一、IP地址

    IP地址 1)网络地址 IP地址由网络号(包括子网号)和主机号组成,网络地址的主机号为全0,网络地址代表着整个网络. 2)广播地址 广播地址通常称为直接广播地址,是为了区分受限广播地址. 广播地址与网 ...

  4. 文档生成工具Sandcastle Help File Builder

    Sandcastle Help File Builder   http://shfb.codeplex.com/

  5. 图片的URL上传至阿里云OSS操作(微信小程序二维码返回的二进制上传到OSS)

    当我们从网络中获取一个URL的图片我们要存储到本地或者是私有的云时,我们可以这样操作  把url中的图片文件下载到本地(或者上传到私有云中)  public String uploadUrlToOss ...

  6. iOS开发之GCD基础

    重新回顾.学习GCD.Block.先贴出一篇不错的讲解GCD基础使用的文章 原文地址:http://blog.csdn.net/aolan1108/article/details/17283415 做 ...

  7. Disruptor之粗糙认识

    一 概述 1.Disruptor Disruptor是一个高性能的异步处理框架,一个“生产者-消费者”模型. 2.RingBuffer RingBuffer是一种环形数据结构,包含一个指向下一个槽点的 ...

  8. Canvas绘制图片模糊

    canvas是html5的新标签,是个可以绘制图形的画布,画布的默认大小为300x150.在自定义绘制画布大小的时候有注意的问题,就是用样式来设置高度和宽度的时候 比如 <div style=& ...

  9. javascript对象(2)

    这个对象,不是那个对象.续更第二篇.. 昨天说了对象的基本概念以及创建,今天来说一下它的其他方法: 1.访问属性的两种方式:点语法.[]语法 var dog =new Object(); dog.na ...

  10. Highcharts error #14: www.highcharts.com/errors/14

    错误原因:数据类型错误,需要的是Number类型,传入的却是String 以为为官网说明: Highcharts Error #14 String value sent to series.data, ...