软谋教育linq to xmlSystemXml Linq命名空间含有19个类下表列出了_第1页
软谋教育linq to xmlSystemXml Linq命名空间含有19个类下表列出了_第2页
软谋教育linq to xmlSystemXml Linq命名空间含有19个类下表列出了_第3页
软谋教育linq to xmlSystemXml Linq命名空间含有19个类下表列出了_第4页
软谋教育linq to xmlSystemXml Linq命名空间含有19个类下表列出了_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论