LINQ To XML体验(基础)

这两天开始学习LINQ to XML的知识,我会继续把自己的感想和示例发布给初学者们学习的,一样欢迎高手们多多指点,请勿使用过激语言,针锋相对,我是个初学者,自知还有许多不足的地方,还请高手们多多耐心指导,好了,下面就开始我们的LINQ to XML旅程吧,在此之前我们需要先了解一下基础知识.

1.LINQ to XML是一种使用XML的新方法,它采用了多种当前使用的XML处理技术,如DOM和XPath,并在.NET Framework内组合为单一的编程接口.
2.LINQ to XML是基于LINQ的,这意味着可以使用LINQ中所有的功能,如标准查询操作符和LINQ编程接口.
3.LINQ to XML由System.Xml.Linq命名空间极其相应的类提供,因此工程中记得添加这个引用哦.

下面列出System.Xml.Linq命名空间中的19个类和基本描述,并且在本章我们一起了解一下最常用到的3个类:XDocument,XElement,XAttribute.之后我们将在下一章以编程示例来讲解如何使用LINQ to XML进行XML操作.

目录

1.System.Xml.Linq简介

2.XElement类

3.XAttribute类

4.XDocument类

 

 

1.System.Xml.Linq简介

说明
Extensions 包含 LINQ to XML 扩展方法。
XAttribute 表示一个 XML 属性。
XCData 表示一个包含 CDATA 的文本节点。
XComment 表示一个 XML 注释。
XContainer 表示可包含其他节点的节点。
XDeclaration 表示一个 XML 声明。
XDocument 表示 XML 文档。
XDocumentType 表示 XML 文档类型定义 (DTD)。
XElement 表示一个 XML 元素。
XName 表示 XML 元素或特性的名称。
XNamespace 表示一个 XML 命名空间。 无法继承此类。
XNode 表示 XML 树中节点的抽象概念(元素、注释、文档类型、处理指令或文本节点)。
XNodeDocumentOrderComparer 包含用于比较节点的文档顺序的功能。 无法继承此类。
XNodeEqualityComparer 比较节点以确定其是否相等。 无法继承此类。
XObject 表示 XML 树中的节点或特性。
XObjectChangeEventArgs 提供有关 ChangingChanged 事件的数据。
XProcessingInstruction 表示 XML 处理指令。
XStreamingElement 表示支持延迟流输出的 XML 树中的元素。
XText 表示一个文本节点。

 

2.XElement类

XElement类表示XML的元素,它是XContainer类的派生类,而XContainer类又派生于XNode类.一个元素是一个节点,因此很多时候会看到它们会交替着使用.XElement是LINQ to XML最重要且最基本的类型之一,因为它包含所有创建和操作XML元素所需要的功能,通过它可以创建元素,添加和修改元素的属性,甚至操作元素的内容.下面就用几个简单的示例来认识一下这个类吧.

代码
//-----------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
//-----------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//创建一个测试树
XElement tree = new XElement("Root",
new XElement("Child1", 1),
new XElement("Child3", 3));
PrintTree(
"创建一个测试树", tree);

//在Child1元素后面添加一个新的元素Child2
tree.Element("Child1").AddAfterSelf(new XElement("Child2", 2));
PrintTree(
"在Child1元素后面添加一个新的元素Child2", tree);

//设置Child1的值为3
tree.SetElementValue("Child1", 3);
PrintTree(
"设置Child1的值为3", tree);

//移除Child1
tree.Element("Child1").Remove();
PrintTree(
"移除Child1", tree);

//保存至文本中
tree.Save("test.xml");
}

private static void PrintTree(string info, XElement tree)
{
Console.WriteLine(info);
Console.WriteLine(tree);
Console.WriteLine(
"----------------------------------");
}
}
}

运行结果:

 

3.XAttribute类

XAttribute类是用来处理XML中元素的属性的,属性是与元素相关联的名称/值对,在操作上和元素有很多相似之处.属性不能作为XML树中的节点,因此不是派生于XNode类.每个属性必须有一个限定名,该名称对元素来说是唯一的.下面就用几个小示例体验一下XAttribute类吧.

代码
//-----------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
//-----------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//创建一个测试树
XElement tree = new XElement("Root",
new XElement("Person"));
PrintTree(
"创建一个测试树", tree);

//为Person元素添加一个Id属性
tree = new XElement("Root",
new XElement("Person",
new XAttribute("Id", 1)));
PrintTree(
"为Person元素添加一个Id属性", tree);

//设置Person元素的Id属性为2
tree.Element("Person").SetAttributeValue("Id", 2);
PrintTree(
"设置Person元素的Id属性为2", tree);

//移除Person元素的Id属性
tree.Element("Person").Attribute("Id").Remove();
PrintTree(
"移除Person元素的Id属性", tree);
}

private static void PrintTree(string info, XElement tree)
{
Console.WriteLine(info);
Console.WriteLine(tree);
Console.WriteLine(
"----------------------------------");
}
}
}

运行结果:

 

4.XDocument类

XDocument类提供了处理XML的方法,包括声明,注释和处理指令,一个XDocument对象可以包括以下内容:

  1.  一个单一XElement对象(根)
  2.  一个单一的XDeclaration对象
  3.  一个单一的XDocumentType对象(指向一个DTD)
  4.  任何数量的XProcessingInstruction对象
  5.  任何数量的XComment对象

不过在LINQ to XML来处理XML时基本不会用到声明,注释和处理指令.
下面就用一个示例来看看如何用XDocument来创建一个简单的XML文档吧,它包含几个元素和一个属性,一个处理指令和一些注释.

代码
//-----------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
//-----------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XDocument doc
= new XDocument(
new XProcessingInstruction("xml-stylesheet", "title='黄聪'"),
new XComment("注释1"),
new XElement("Root",
new XElement("Persons",
new XElement("Person",
new XAttribute("Id", 1),
new XElement("Name", "Huang Cong"),
new XElement("Sex", ""))),
new XComment("注释2")));

doc.Save(
"test.xml");
}
}
}

运行结果:

 

小结:

以上为Linq to XML的基本知识,希望和我一样想学习Linq to XML的朋友把这些好好掌握了,这样我们就可以往Linq to XML体验(编程篇)前进啦~~~

作者: 小聪崽的一切 发表于 2010-12-31 01:23 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"