您好,欢迎访问一九零五行业门户网

LINQ to XML 编程基础的图文代码详细介绍

1、linq to xml类
以下的代码演示了如何使用linq to xml来快速创建一个xml:
隐藏行号 复制代码 ? 创建 xml
public static void createdocument() { string path = @"d:\website"; xdocument xdoc = new xdocument(new xdeclaration("1.0", "utf-8", "yes"), new xelement("root", "root")); xdoc.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <root>root</root>
2、xelement类xelement 类是 linq to xml 中的基础类之一。 它表示一个 xml 元素。 可以使用该类创建元素;更改元素内容;添加、更改或删除子元素;向元素中添加属性;或以文本格式序列化元素内容。 还可以与 system.xml 中的其他类(例如 xmlreader、xmlwriter 和 xslcompiledtransform)进行互操作。
使用linq to xml创建xml文档有很多种方式,具体使用哪种方法要根据实际需要。而创建xml文档最简单、最常见的方式是使用xelement类。以下的代码演示了如何使用xelement类创建一个xml文档:
显示行号 复制代码 ? 这是一段程序代码。
public static void createcategories() { string path = @"d:\website"; xelement root = new xelement("categories", new xelement("category", new xelement("categoryid", guid.newguid()), new xelement("categoryname", "beverages") ), new xelement("category", new xelement("categoryid", guid.newguid()), new xelement("categoryname", "condiments") ), new xelement("category", new xelement("categoryid", guid.newguid()), new xelement("categoryname", "confections") ) ); root.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <categories> <category> <categoryid>57485174-46fc-4e8c-8d98-d25b53d504a1</categoryid> <categoryname>beverages</categoryname> </category> <category> <categoryid>1474dde1-8014-48f7-b093-b47ca5d5b770</categoryid> <categoryname>condiments</categoryname> </category> <category> <categoryid>364224e0-e002-4939-90fc-0fd93e0cf35b</categoryid> <categoryname>confections</categoryname> </category> </categories>
xelement类包含了许多方法,这些方法使得处理xml变得轻而易举。有关这些方法请参照msdn。
其中,save、createreader、tostring和writeto方法是比较常用的三个方法:
3、xattribute类 xattribute类用来处理元素的属性,属性是与元素相关联的“名称-值”对,每个元素中不能有名称重复的属性。使用xattribute类与使用xelement类的操作十分相似,下面的示例演示了如何在创建xml树时为其添加一个属性:
显示行号 复制代码 ? 这是一段程序代码。
public static xelement createcategoriesbyxattribute() { xelement root = new xelement("categories", new xelement("category", new xattribute("categoryid", guid.newguid()), new xelement("categoryname", "beverages") ), new xelement("category", new xattribute("categoryid", guid.newguid()), new xelement("categoryname", "condiments") ), new xelement("category", new xattribute("categoryid", guid.newguid()), new xelement("categoryname", "confections") ) ); root.save(path); return root; }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <categories> <category categoryid="a6d5ef04-3f83-4e00-aeaf-52444add7570"> <categoryname>beverages</categoryname> </category> <category categoryid="67a168d5-6b22-4d82-9bd4-67bec88c2ccb"> <categoryname>condiments</categoryname> </category> <category categoryid="17398f4e-5ef1-48da-8a72-1c54371b8e76"> <categoryname>confections</categoryname> </category> </categories>
xattribute类的方法比较少,常用的三个是:
以下的示例使用remove来删除第一个元素的categoryid属性:
显示行号 复制代码 ? 这是一段程序代码。
public static void removeattribute() { xelement xdoc = createcategoriesbyxattribute(); xattribute xattr = xdoc.element("category").attribute("categoryid"); xattr.remove(); xdoc.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <categories> <category> <categoryname>beverages</categoryname> </category> <category categoryid="5c311c1e-ede5-41e5-93f7-5d8b1d7a0346"> <categoryname>condiments</categoryname> </category> <category categoryid="bfde8db5-df84-4415-b297-cd04d8db9712"> <categoryname>confections</categoryname> </category> </categories>
作为尝试,试一试以下删除属性的方法:
public static void removeattributebydoc() { xelement xdoc = createcategoriesbyxattribute(); xattribute xattr = xdoc.attribute("categoryid"); xattr.remove(); xdoc.save(path); }
运行该示例将会抛出一个空引用异常,因为元素categories没有一个叫做categoryid的属性。
4、xdocument类
xdocument类提供了处理xml文档的方法,包括声明、注释和处理指令。一个xdocument对象可以包含以下内容:
下面的示例创建了一个简单的xml文档,它包含几个元素和一个属性,以及一个处理指令和一些注释:
public static void createxdocument() { xdocument xdoc = new xdocument( new xprocessinginstruction("xml-stylesheet", "title='empinfo'"), new xcomment("some comments"), new xelement("root", new xelement("employees", new xelement("employee", new xattribute("id", "1"), new xelement("name", "scott klein"), new xelement("title", "geek"), new xelement("hiredate", "02/05/2007"), new xelement("gender", "m") ) ) ), new xcomment("more comments") ); xdoc.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet title='empinfo'?> <!--some comments--> <root> <employees> <employee id="1"> <name>scott klein</name> <title>geek</title> <hiredate>02/05/2007</hiredate> <gender>m</gender> </employee> </employees> </root> <!--more comments-->
xdocument类包含多个与xelement类相同的方法,具体内容可以参阅msdn。需要注意的是,处理节点和元素的大部分功能都可以通过xelement获得,只有当绝对需要文档层次的处理能力,以及需要访问注释、处理指令和声明时,才有使用xdocument类的必要。
创建了xml文档后,可以使用nodesafterself方法返回指定的xelement元素之后的所有同级元素。需要注意的是,此方法只包括返回集合中的同级元素,而不包括子代。此方法使用延迟执行。以下代码演示了这一过程:
显示行号 复制代码 ? 这是一段程序代码。
public static void nodesafterself() { xelement root = new xelement("categories", new xelement("category", new xelement("categoryid", guid.newguid()), new xelement("categoryname", "食品"), new xelement("description", "可以吃的东西") ) ); foreach (var item in root.element("category").element("categoryid").nodesafterself()) { console.writeline((item as xelement).value); } }
二、linq to xml编程概念
本节将介绍linq to xml编程的相关概念,例如如何加载xml、创建全新xml、操纵xml的信息以及遍历xml文档。
1、加载已有的xml使用linq to xml加载xml可以从多种数据源获得,例如字符串、xmlreader、textreader或文件。
下面的示例演示了如何从文件中加载xml:
public static void loadfromfile() { xelement root = xelement.load(path); console.writeli
也可以使用parse方法从一个字符串加载xml:
public static void loadfromstring() { xelement root = xelement.parse(@" <categories> <category> <categoryid>1</categoryid> <categoryname>beverages</categoryname> <description>soft drinks, coffees, teas, beers, and ales</description> </category> </categories> "); console.writeline(root.tostring()); }
2、保存xml在前面的示例中曾多次调用xelement对象的save方法来保存xml文档,在这里就不冗述了。
3、创建xml在前面的示例中曾多次调用xelement对象的构造函数来创建xml文档,在这里就不冗述了。需要说明的是,在使用linq to xml创建xml文档时,会有代码缩进,这使代码的可读性大大加强。
4、遍历xml使用linq to xml在xml树中遍历xml是相当简单的。只需要使用xelement和xattribute类中所提供的方法。elements和element方法提供了定位到某个或某些元素的方式。下面的示例演示了如何遍历xml树,并获取指定元素的方式:
public static void enum() { xelement root = new xelement("categories"); using (northwinddatacontext db = new northwinddatacontext()) { root.add( db.categories .select ( c => new xelement ( "category" , new xelement("categoryname", c.categoryname) ) ) ); } foreach (var item in root.elements("category")) { console.writeline(item.element("categoryname").value); } }
上述代码运行的结果为:
是不是很简单呢?nodes()、elements()、element(name)和elements(name)方法为xml树的导航提供了基本功能。
5、操纵xmllinq to xml一个重要的特性是能够方便地修改xml树,如添加、删除、更新和复制xml文档的内容。
i.插入
使用xnode类的插入方法可以方便地向xml树添加内容:
在下面的示例中,使用addafterself方法向现有xml中添加一个新节点:
public static void addafterself() { xelement root = xelement.parse(@" <categories> <category> <categoryid>1</categoryid> <categoryname>beverages</categoryname> <description>soft drinks, coffees, teas, beers, and ales</description> </category> </categories> "); xelement xele = root.element("category").element("categoryname"); xele.addafterself(new xelement("adddate", datetime.now)); root.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <categories> <category> <categoryid>1</categoryid> <categoryname>beverages</categoryname> <adddate>2010-01-31t03:08:51.813736+08:00</adddate> <description>soft drinks, coffees, teas, beers, and ales</description> </category> </categories>
当需要添加一个元素到指定节点之前时,可以使用addbeforeself方法。
ii.更新
在linq to xml中更新xml内容可以使用以下几种方法:
在下面的示例中使用了replacewith与setelementvalue方法对xml进行了更新操作:
public static void update() { xelement root = xelement.parse(@" <categories> <category> <categoryid>1</categoryid> <categoryname>beverages</categoryname> <description>soft drinks, coffees, teas, beers, and ales</description> </category> </categories> "); root.element("category").element("categoryid").replacewith(new xelement("id", "2")); root.element("category").setelementvalue("categoryname", "test data"); root.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <categories> <category> <id>2</id> <categoryname>test data</categoryname> <description>soft drinks, coffees, teas, beers, and ales</description> </category> </categories>
iii.删除
可以使用remove(xelement)与removeall方法来删除xml。
在下面的示例中,使用了removeall方法:
} public static void remove() { string path = @"d:\"; xelement root = xelement.parse(@" <categories> <category> <categoryid>1</categoryid> <categoryname>beverages</categoryname> <description>soft drinks, coffees, teas, beers, and ales</description> </category> </categories> "); root.removeall(); root.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <categories />
在下面的示例中,使用了remove方法删除了xml的description元素:
public static void remove() { xelement root = xelement.parse(@" <categories> <category> <categoryid>1</categoryid> <categoryname>beverages</categoryname> <description>soft drinks, coffees, teas, beers, and ales</description> </category> </categories> "); root.element("category").element("description").remove(); root.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <categories> <category> <categoryid>1</categoryid> <categoryname>beverages</categoryname> </category> </categories>
6、处理属性i.添加
linq to xml添加属性与添加元素师类似的,可以使用构造函数或者add方法来添加属性:
public static void addattribute() { xelement root = new xelement("categories", new xelement("category", new xattribute("categoryid", "1"), new xelement("categoryname", "beverages"), new xelement("description", "soft drinks, coffees, teas, beers, and ales") ) ); root.element("category").add(new xattribute("adddate", datetime.now.toshortdatestring())); root.save(path); }
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <categories> <category categoryid="1" adddate="2010-01-31"> <categoryname>beverages</categoryname> <description>soft drinks, coffees, teas, beers, and ales</description> </category> </categories>
ii.检索
检索属性可以使用attribute(name)方法:
显示行号 复制代码 ? 这是一段程序代码。
public static void selectattribute() { xelement root = new xelement("categories", new xelement("category", new xattribute("categoryid", "1"), new xelement("categoryname", "beverages"), new xelement("description", "soft drinks, coffees, teas, beers, and ales") ) ); xattribute xattr = root.element("category").attribute("categoryid"); console.writeline(xattr.name); console.writeline(xattr.value); }
上述代码的运行结果为:
categoryid 1
本文总结本文介绍了linq to xml的编程基础,即system.xml.linq命名空间中的多个linq to xml类,这些类都是linq to xml的支持类,它们使得处理xml比使用其他的xml工具容易得多。在本文中,着重介绍的是xelement、xattribute和xdocument。
以上就是linq to xml 编程基础的图文代码详细介绍的内容。
其它类似信息

推荐信息