




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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文件,其内容为: 57485174-46fc-4e8c-8d98-d25b53d504a1 Beverages 1474dde1-8014-48f7-b093-b47ca5d5b770 Condiments 364224e0-e002-4939-90fc-0fd93e0cf35b Confections 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文件,其内容为: Beverages Condiments Confections XAttribute类的方法比较少,常用的三个是: 以下的示例使用Remove来删除第一个元素的CategoryID属性:显示行号 复制代码 ? 这是一段程序代码。 public static void RemoveAttribute() XElement xdoc = CreateCategoriesByXAttribute(); XAttribute xattr = xdoc.Element(Category).Attribute(CategoryID); xattr.Remove(); xdoc.Save(path);运行该示例将会得到一个xml文件,其内容为: Beverages Condiments Confections 作为尝试,试一试以下删除属性的方法:显示行号 复制代码 ? 这是一段程序代码。 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文件,其内容为: Scott Klein Geek 02/05/2007 M 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.WriteLine(root.ToString();也可以使用Parse方法从一个字符串加载xml:显示行号 复制代码 ? 这是一段程序代码。 public static void LoadFromString() XElement root = XElement.Parse( 1 Beverages Soft drinks, coffees, teas, beers, and ales ); 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( 1 Beverages Soft drinks, coffees, teas, beers, and ales ); XElement xele = root.Element(Category).Element(CategoryName); xele.AddAfterSelf(new XElement(AddDate, DateTime.Now); root.Save(path);运行该示例将会得到一个xml文件,其内容为: 1 Beverages 2010-01-31T03:08:51.813736+08:00 Soft drinks, coffees, teas, beers, and ales 当需要添加一个元素到指定节点之前时,可以使用AddBeforeSelf方法。 II.更新 在LINQ to XML中更新xml内容可以使用以下几种方法: 在下面的示例中使用了ReplaceWith与SetElementValue方法对xml进行了更新操作:显示行号 复制代码 ? 这是一段程序代码。 public static void Update() XElement root = XElement.Parse( 1 Beverages Soft drinks, coffees, teas, beers, and ales ); root.Element(Category).Element(CategoryID).ReplaceWith(new XElement(ID, 2); root.Element(Category).SetElementValue(CategoryName, test data); root.Save(path);运行该示例将会得到一个xml文件,其内容为: 2 test data Soft drinks, coffees, teas, beers, and ales III.删除 可以使用Remove(XElement)与RemoveAll方法来删除xml。 在下面的示例中,使用了RemoveAll方法:显示行号 复制代码 ? 这是一段程序代码。 public static void Remove() string path = d:; XElement root = XElement.Parse( 1 Beverages Soft drinks, coffees, teas, beers, and ales ); root.RemoveAll(); root.Save(path); 运行该示例将会得到一个xml文件,其内容为:在下面的示例中,使用了Remove方法删除了xml的Description元素: 显示行号 复制代码 ? 这是一段程序代码。 public static void Remove() XElement root = XElement.Parse( 1 Beverages Soft drinks, coffees, teas, beers, and ales ); root.Element(Category).Element(Description).Remove(); root.Save(path);运行该示例将会得到一个xml文件,其内容为: 1 Beverages 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);运
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 消防百科题目及答案
- 测井车司机培训考试题及答案
- 现在进行时题目及答案
- 曹县社工考试题型分布及答案
- 葡萄病虫害防法知识培训课件
- 2025采购合同承包方式
- 农村循环经济合作模式设计协议
- 新能源动力电池极片冲切模具研发生产项目可行性研究报告模板-立项备案
- 物联概论试题及答案
- 2025年搅拌车租赁合同范本
- 2024年溧阳市卫生健康系统农村订单定向医学毕业生定向招聘笔试真题
- 执行力责任心培训课件
- 2025年营养师考试冲刺押题试卷:营养师实操技能考核与解析
- 水厂设施现代化改造方案
- 2025秋季开学第一课完整版课件
- 第2课《中国人首次进入自己的空间站》教学设计统编版八年级语文上册
- 2025重庆对外建设集团招聘41人笔试参考题库附答案解析
- 2025年版小学数学新课程标准测试题含答案【附新课标解读】
- 中医健康管师试题及答案
- 投标造价委托协议书范本
- 新粒子生成与生长机制-洞察及研究
评论
0/150
提交评论