版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
LINQtoXML1、LINQtoXML类XMLCDATAXMLXMLXXMLXMLXMLXML表示一个XML名空XML XNodeXAttribute为Changing和Changed提供数XMLLINQtoXMLpublicpublicstaticvoid{Xxdoc=new(newXDeclaration("1.0","utf-8","yes"),newXElement("Root","root")}xml<?xml<?xmlversion="1.0"encoding="utf-8"LINQ上投入了很大的精力,使我们在编程时感觉到很舒服。下面将详细介绍处理XML时使用最多的三个类:XElement、XAttribute和X。如果掌握了这些类,使LINQtoXML时将会感到很顺手。XElementLINQtoXMLXMLSystem.Xml中的其他类(XmlReader、XmlWriter和piledTransform)LINQtoXMLxml文档有很多种方式,具体使用哪种方法要根据实际需要。而创建建一个xml文档:publicpublicstaticvoid{XElementroot=newnewnewnewXElement("CategoryID",Guid.NewGuid()),newXElement("CategoryName","Beverages")newnewXElement("CategoryID",Guid.NewGuid()),newXElement("CategoryName","Condiments")newnewXElement("CategoryID",Guid.NewGuid()),newXElement("CategoryName","Confections"))}xml<?xml<?xmlversion="1.0"encoding="utf-LINQtoXMLLINQtoSQLLINQtoObject获取数据源,然后填充到xml树。以下的示例从Northwind数据库中Categories、Products表中的数据来创建包含产品类别,以及每个类别下所有产品的xml文件:publicpublicstaticvoid{using ontextdb=newNorthwindD{XElementroot=newXElement("Categories",c=>newXElement,newXElement("CategoryID",,new,newXElement,p=>newXElement,newment("ProductName",))))))}}xml<?xml<?xmlversion="1.0"encoding="utf-<ProductName>Aniseed<ProductName>ChefAnton'sCajun<ProductName>TeatimeChocolateXElement类包含了许多方法,这些方法使得处理xmlMSDN其中,Save、CreateReader、ToString和WriteTo无TextWriterXmlWriterXmlWriter无XML在创建xml树时为其添加一个属性:publicpublicstaticXElement{XElementroot=newXElement("Categories",newXElement("Category",newXAttribute("CategoryID",newnewXElement("CategoryName",newnewXAttribute("CategoryID",Guid.NewGuid()),newXElement("CategoryName","Condiments")newnewXAttribute("CategoryID",Guid.NewGuid()),newXElement("CategoryName","Confections"))returnroot;}xml<?xml<?xmlversion="1.0"encoding="utf-<CategoryCategoryID="a6d5ef04-3f83-4e00-aeaf-<CategoryCategoryID="67a168d5-6b22-4d82-9bd4-<CategoryCategoryID="17398f4e-5ef1-48da-8a72-XAttribute以下的示例使用RemoveCategoryIDpublicpublicstaticvoid{XElementxdoc=XAttributexattr=xdoc.Element("Category").Attribute("CategoryID");}xml<?xml<?xmlversion="1.0"encoding="utf-<CategoryCategoryID="5c311c1e-ede5-41e5-93f7-<CategoryCategoryID="bfde8db5-df84-4415-b297-publicpublicstaticvoid{XElementxdoc=CreateCategoriesByXAttribute();XAttributexattr=xdoc.Attribute("CategoryID");}4、XX类提供了处理xml文档的方法,包括、注释和处理指令。一个xmlXml注释。它不能是第一个参数,因为一个有效的xml文档不为处理xmlxml文档,它包含几个元素和一个属性,以及一个处理指令和一publicpublicstaticvoid{Xxdoc=new(newXProcessingInstruction("xml-stylesheet",newXComment("somenewnewnewnewnew,new,"Scottnew",newnewr",))newXComment("more}xml<?xml<?xmlversion="1.0"encoding="utf-<?xml-stylesheet<!--somecomments--<Employee<Name>Scott<!--morecomments--X类包含多个与XElement类相同的方法,具体内容可以参阅MSDN。需要注意XElement获得,只有当绝对需要文档层次的处理能力,以及需要注释、处理指令和时,才有使用X类的必要。创建了xml文档后,可以使用NodesAfterSelf方法返回指定的XElement元后的所有同publicpublicstaticvoid{XElementroot=newXElement("Categories",newXElement("Category",newXElement("CategoryID",Guid.NewGuid()),newXElement("CategoryName","食品"),newXElement("Description",)foreach(varitemin{ ine((itemas}}使用LINQtoXML中的类来处理xml十分简单和高效,包括创建、查询和xml二、LINQtoXML本节将介绍LINQtoXML编程的相关概念,例如如何加载xml、创建全新xml、xml的xml文档。publicpublicstaticvoid{XElementroot=XElement.Load(path); }Parse方法从一个字符串加载publicpublicstaticvoid{XElementroot= }2、保存面的示例中调用XElement对象的Save方法来保存xml文档在这里就不冗述了面的示例中调用XElement对象的构造函数来创建xml文档,在这里就不冗述了。LINQtoXMLxml文档时,会有代码缩进,这使代码的可读性大大加强。LINQtoXML在xml树中遍历xmlXElementXAttribute类中所提供的方法。ElementsElement方法提供了定位到某个或某些元素的方式。下面的示例演示了如何遍历xml树,并获取指定元素的方式:publicpublicstaticvoid{XElementroot=newusing(NorthwindDontextdb=newNorthwindD{c=>newXElement,new))}foreach(varitemin{ }}}}5、LINQtoXML一个重要的特性是能够方便地修改xml树,如添加、删除、更新和xml文XNode类的插入方法可以方便地向xmlpublicpublicstaticvoid{XElementroot=XElementxele=root.Element("Category").Element("CategoryName");xele.AddAfterSelf(newXElement("AddDate",DateTime.Now));}xml<?xml<?xmlversion="1.0"encoding="utf-<Description>Softdrinks,coffees,teas,beers,andLINQtoXMLxmlReReReSe在下面的示例中使用了ReceWith与SeementValue方法对xml进行了更新操作publicpublicstaticvoid{XElementroot=root.Element("Category").Element("CategoryID").ReceWith(newXElement("ID", ementValue("CategoryName","testdata");}xml<?xml<?xmlversion="1.0"encoding="utf-<CategoryName>test<Description>Softdrinks,coffees,teas,beers,andpublicstaticvoid{XElementrootpublicstaticvoid{XElementroot=}xml<?xml<?xmlversion="1.0"encoding="utf-<CategoriesRemove方法删除了xml的Descriptionpublicpublicstaticvoid{XElementroot=}xml<?xml<?xmlversion="1.0"encoding="utf-LINQtoXMLAddpublicpublicstaticvoid{XElementroot=newXElement("Categories",newXElement("Category",newnewnewXElement("Description","Softdrinks,coffees,teas,beers,)root.Element("Category").Add(newXAttribute("AddDate",}xml<?xml<?xmlversion="1.0"encoding="utf-<CategoryCategoryID="1"AddDate="2010-01-<Description>Softdrinks,coffees,teas,beers,andpublicpublicstati
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 民政职业技能鉴定(墓地管理员、殡葬管理)考试题库及答案
- 2026年一级注册建筑师之建筑设计考试题库及答案
- 2026年行车工考试题库及答案
- 2026年植物生长模拟试题模拟试卷带答案
- 2026年渭柳中学笔试题库及答案
- 2026年养老财务规划师考试题库及参考答案
- 水利五大员施工员质检员资料员材料员安全员考试题库带答案
- 2026年驾考宝典科一题库及答案
- 2026年15二建水利模拟试题带答案
- 2026年叉车锦州模拟考试卷带答案
- 2026年电厂化学设备检修工专项题库(附答案与解释)
- 高中物理选择性必修2-基础知识自测小纸条(含答案)
- 气管切开患者气管造口周围皮肤护理
- 2025年中国铁路南宁局校招真题及答案
- 子宫肌瘤的课件
- 2026年美丽中国全国国家版图知识竞赛测试题库
- 2026海南农垦投资控股集团秋招试题及答案
- 智慧育种课件
- 国企招聘面试流程与技巧分享
- 高考英语词汇 40篇短文搞定高考英语3500单词
- 老年急性胆道感染诊治专家共识(2025版)解读 3
评论
0/150
提交评论