Fork me on GitHub

LINQ To XML

议程

1.XML&LINQ To XML

2.LINQ To XML的类

3.LINQ To XML操作

XML简介

XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Markup Language,标准通用标记语言)。Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具。扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然XML占用的空间比二进制数据要占用更多的空间,但XML极其简单易于掌握和使用。

什么是LINQ To XML

LINQ to XML 是一种启用了 LINQ 的内存 XML 编程接口,使用它,可以在 .NET Framework 编程语言中处理 XML。

它将 XML 文档置于内存中,这一点很像文档对象模型 (DOM)。 您可以查询和修改 XML 文档,修改之后,可以将其另存为文件,也可以将其序列化然后通过网络发送。 但是,LINQ to XML 与 DOM 不同:它提供一种新的对象模型,这是一种更轻量的模型,使用也更方便,这种模型利用了 Visual C# 2008 在语言方面的改进。

LINQ to XML 最重要的优势是它与 Language-Integrated Query (LINQ) 的集成。 由于实现了这一集成,因此,可以对内存 XML 文档编写查询,以检索元素和属性的集合。 LINQ to XML 的查询功能在功能上(尽管不是在语法上)与 XPath 和 XQuery 具有可比性。 Visual C# 2008 集成 LINQ 后,可提供更强的类型化功能、编译时检查和改进的调试器支持。

LINQ To XML类

LINQ To XML类位于System.Xml.Linq命名空间下。

下面是常用的18个类

1.XAttribute 表示一个 XML 属性。

2.XCData 表示一个 CDATA 文本节点。

3.XComment 表示一个 XML 注释。

4.XContainer 是适用于可能具有子节点的所有节点的抽象基类。 下面的类派生自 XContainer 类:

•XElement

•Xdocument

5.XDeclaration 表示一个 XML 声明。 XML 声明用于声明 XML 版本和文档的编码。 此外,XML 声明还指定 XML 文档是否为独立文档。

6.XDocument 表示一个 XML 文档。

7.XDocumentType 表示一个 XML 文档类型定义 (DTD)。

8.XElement 表示一个 XML 元素。

9.XName 表示元素 (XElement) 和属性 (XAttribute) 的名称。

10.XNamespace 表示 XElement 或 XAttribute 的命名空间。 命名空间是 XName 的一个组件。

11.XNode 是一个抽象类,它表示 XML 树的节点。 下面的类派生自 XNode 类:

•XText

•XContainer

•XComment

•XProcessingInstruction

•XDocumentType

12.XNodeDocumentOrderComparer 提供用于比较节点的文档顺序的功能。

13.XNodeEqualityComparer 提供用于比较节点的值是否相等的功能。

14.XObject 是 XNode 和 XAttribute 的抽象基类。 它提供批注和事件功能。

15.XObjectChange 指定对 XObject 引发事件时的事件类型。

16.XObjectChangeEventArgs 为 Changing 和 Changed 事件提供数据。

17.XProcessingInstruction 表示一个 XML 处理指令。 处理指令将信息传递给处理 XML 的应用程序。

18.XText 表示一个文本节点。 多数情况下都不必使用此类。 此类主要用于混合内容。

LINQ To XML类——XDocument

下面重点学习三个类:XDocument,XElement,Xattribute

  • XDocument类:表示一个 XML 文档。

XDocument 可以包含以下元素:

•一个 XDeclaration 对象。XDeclaration 使您能够指定 XML 声明的相关部分: XML 版本、文档的编码,以及 XML 文档是否是独立的。

•一个 XElement 对象。 这是 XML 文档的根节点。

•任意数目的 XProcessingInstruction 对象。 处理指令将信息传递给处理 XML 的应用程序。

•任意数目的 XComment 对象。 注释将与根元素同级。 XComment 对象不能是列表中的第一个参数,因为 XML 文档以注释开头无效。

•一个用于 DTD 的 XDocumentType。

用XDocument创建XML文件

C#代码:

XDocument d = new XDocument( new XDeclaration("1.0", "utf-8", "true"),

new XComment("This is a comment."),

new XProcessingInstruction("xml-stylesheet", "href='mystyle.css' title='Compact' type='text/css'"),

new XElement("Pubs",

new XElement("Book",

new XElement("Title", "Artifacts of Roman Civilization"),

new XElement("Author", "Moreno, Jordao")

) ) );

Console.WriteLine(d.Declaration );

Console.WriteLine(d);

XML文件:

<?xml version="1.0" encoding="utf-8" standalone="true"?>

<!--This is a comment.-->

<?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>

<Pubs>

<Book>

<Title>Artifacts of Roman Civilization</Title>

<Author>Moreno, Jordao</Author>

</Book>

</Pubs>

  • XElement类:表示一个 XML 元素。

XDocument 可以包含以下元素:

•Xelement

•Xcomment

•XprocessingInstruction

•XText

用XElement创建XML文件

C#代码:

XElement xml1 = new XElement("Root",

new XElement("Node1", 1),

new XElement("Node2", 2),

new XElement("Node3", 3),

new XElement("Node4", 4),

new XElement("Node5", 5),

new XElement("Node6", 6)

);

XElement xml2 = new XElement("Root",

from el in xml1.Elements()

where ((int)el >= 3 && (int)el <= 5)

select el

);

Console.WriteLine(xml2);

XML文件:

<Root>

<Node3>3</Node3>

<Node4>4</Node4>

<Node5>5</Node5>

</Root>

  • XAttribute类:属性是与元素关联的名称/值对。 XAttribute 类表示 XML 属性。

属性与元素之间有些区别。XAttribute 对象不是 XML 树中的节点。 它们是与 XML 元素关联的名称/值对。 与文档对象模型 (DOM) 相比,这更加贴切地反映了 XML 结构。 虽然 XAttribute 对象实际上不是 XML 树的节点,但使用 XAttribute 对象与使用 XElement 对象非常相似。

XElement phone = new XElement("Phone",

new XAttribute("Type", "Home"),

"555-555-5555");

Console.WriteLine(phone);

LINQ To XML——XML操作

加载和保存XML

XElement City = XElement.Load("F://City.xml");

City.Save("F://City1.xmla");

创建XML

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age", 20)),

new XElement("Student",

new XElement("Name", "李四"),

new XElement("Sex", "女"),

new XElement("Age", 19))

);

Console.WriteLine(Students);

编历XML

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age", 20)),

new XElement("Student",

new XElement("Name", "李四"),

new XElement("Sex", "女"),

new XElement("Age", 19))

);

foreach (XNode node in Students.Nodes())

{

Console.WriteLine(node);

Console.WriteLine("----------------------------");

}

foreach (XElement ele in Students.Elements())

{

Console.WriteLine(ele);

Console.WriteLine("********************************");

}

添加XML节点

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age", 20)),

new XElement("Student",

new XElement("Name", "李四"),

new XElement("Sex", "女"),

new XElement("Age", 19))

);

foreach (XElement ele in Students.Elements())

{

ele.Element("Age").AddAfterSelf(new XElement("Hight", 173));

ele.Element("Age").AddBeforeSelf(new XElement("Weight", 73));

ele.Add (new XElement("Hobby", "Coding"));

}

Console.WriteLine(Students)

更新XML节点

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age",new XAttribute ("Year",1989/8/22), 20))

);

Students.Element(“Student”).Element(“Age”).ReplaceWith(new XElement(“Age”, 28));//替换掉整个节点

// Students.Element(“Student”).Element(“Age”).ReplaceNodes ( 28);//只替换节点值

// Students.Element(“Student”).Element(“Age”).ReplaceAll (28);//替换掉整个节点

Console.WriteLine(Students);

删除XML节点

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age",new XAttribute ("Year","1989/8/22"), 20))

);

//Students.Element("Student").Element("Age").Remove ();//移除节点

//Students.Element("Student").Element("Age").RemoveAll();//移除节点的值和属性

Students.Element("Student").Element("Age").RemoveNodes();//移除节点的值

Console.WriteLine(Students);

添加XML属性

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age",new XAttribute ("Year","1989/8/22"), 20))

);

Students.Element("Student").SetAttributeValue("dd","dddd");

Console.WriteLine(Students);

更新XML属性

Students.Element("Student").Element("Age").ReplaceAttributes(new XAttribute("Year","dd"));

Students.Element("Student").Element("Age").SetAttributeValue("Year", "dddd");

删除XML属性

Students.Element("Student").Element("Age").Attribute("Year").Remove ();

Students.Element("Student").Element("Age").RemoveAttributes ();

遍历XML属性

var Attr = from att in Students.Element("Student").Element("Age").Attributes()

select att;

foreach (var att in Attr)

{

Console.WriteLine(att);

}

LINQ To XML——Annotation

可以给Xelement或Xattribute添加备注。备注不会保存在xml文件中。

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age",new XAttribute ("Year","1989/8/22"), 20))

);

Students.AddAnnotation(“这是一个学生的XML”);//这里的参数不只是string,可以是任何类型,并且可以添中很多个备注

Console.WriteLine(Students .Annotation (typeof (string)));

LINQ To XML——定位方法

  • Ancestors:返回指定节点的所有上级元素。(AncestorsAndSelf:返回包括自己的所有上级元素)

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age",new XAttribute ("Year","1989/8/22"), 20))

);

foreach (var el in Students.Element ("Student").Element("Sex").Ancestors ())

{

Console.WriteLine(el);

}

  • Descendants:返回所有指定元素名称的后续所有相同元素集合。

(DescendantsAndSelf包括元素本身。)

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age", new XAttribute("Year", "1989/8/22"), 20)),

new XElement("Student",

new XElement("Name", "李四"),

new XElement("Sex", "女"),

new XElement("Age", new XAttribute("Year", "1990/8/22"), 20))

);

foreach (var v in Students.Descendants())

{

Console.WriteLine(v);

Console.WriteLine("-----------------------");

}

  • ElementsAfterSelf:返回当前节点后的所有节点。(ElementsBeforeSelft:返回当前节点前的所有节点)

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age", new XAttribute("Year", "1989/8/22"), 20)),

new XElement("Student",

new XElement("Name", "李四"),

new XElement("Sex", "女"),

new XElement("Age", new XAttribute("Year", "1990/8/22"), 20))

);

foreach (var v in Students.Element ("Student").ElementsAfterSelf ())

{

Console.WriteLine(v);

Console.WriteLine("-----------------------");

}

LINQ To XML——事件

LINQ To XML提供了两个事件,对元素的changing和changed。

XElement Students = new XElement("Students",

new XElement("Student",

new XElement("Name", "张三"),

new XElement("Sex", "男"),

new XElement("Age", new XAttribute("Year", "1989/8/22"), 20)),

new XElement("Student",

new XElement("Name", "李四"),

new XElement("Sex", "女"),

new XElement("Age", new XAttribute("Year", "1990/8/22"), 20))

);

Students.Changing += delegate(object sender, XObjectChangeEventArgs e)

{

Console.WriteLine(e.ObjectChange.ToString());

};

Students.Changed += delegate(object sender, XObjectChangeEventArgs e)

{

Console.WriteLine(e.ObjectChange.ToString());

};

Students.SetAttributeValue("a", "aaa");

Console.WriteLine(Students);

posted @ 2009-08-24 10:32  桂素伟  阅读(1081)  评论(0编辑  收藏  举报