軟謀教育linq to xmlSystemXml Linq命名空間含有19個(gè)類下表列出了_第1頁
軟謀教育linq to xmlSystemXml Linq命名空間含有19個(gè)類下表列出了_第2頁
軟謀教育linq to xmlSystemXml Linq命名空間含有19個(gè)類下表列出了_第3頁
軟謀教育linq to xmlSystemXml Linq命名空間含有19個(gè)類下表列出了_第4頁
軟謀教育linq to xmlSystemXml Linq命名空間含有19個(gè)類下表列出了_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

LINQtoXML1、LINQtoXML類XMLCDATAXMLXMLXXMLXMLXMLXML表示一個(gè)XML名空XML XNodeXAttribute為Changing和Changed提供數(shù)XMLLINQtoXMLpublicpublicstaticvoid{Xxdoc=new(newXDeclaration("1.0","utf-8","yes"),newXElement("Root","root")}xml<?xml<?xmlversion="1.0"encoding="utf-8"LINQ上投入了很大的精力,使我們在編程時(shí)感覺到很舒服。下面將詳細(xì)介紹處理XML時(shí)使用最多的三個(gè)類:XElement、XAttribute和X。如果掌握了這些類,使LINQtoXML時(shí)將會感到很順手。XElementLINQtoXMLXMLSystem.Xml中的其他類(XmlReader、XmlWriter和piledTransform)LINQtoXMLxml文檔有很多種方式,具體使用哪種方法要根據(jù)實(shí)際需要。而創(chuàng)建建一個(gè)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獲取數(shù)據(jù)源,然后填充到xml樹。以下的示例從Northwind數(shù)據(jù)庫中Categories、Products表中的數(shù)據(jù)來創(chuàng)建包含產(chǎn)品類別,以及每個(gè)類別下所有產(chǎn)品的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在創(chuàng)建xml樹時(shí)為其添加一個(gè)屬性: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文檔的方法,包括、注釋和處理指令。一個(gè)xmlXml注釋。它不能是第一個(gè)參數(shù),因?yàn)橐粋€(gè)有效的xml文檔不為處理xmlxml文檔,它包含幾個(gè)元素和一個(gè)屬性,以及一個(gè)處理指令和一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類包含多個(gè)與XElement類相同的方法,具體內(nèi)容可以參閱MSDN。需要注意XElement獲得,只有當(dāng)絕對需要文檔層次的處理能力,以及需要注釋、處理指令和時(shí),才有使用X類的必要。創(chuàng)建了xml文檔后,可以使用NodesAfterSelf方法返回指定的XElement元后的所有同publicpublicstaticvoid{XElementroot=newXElement("Categories",newXElement("Category",newXElement("CategoryID",Guid.NewGuid()),newXElement("CategoryName","食品"),newXElement("Description",)foreach(varitemin{ ine((itemas}}使用LINQtoXML中的類來處理xml十分簡單和高效,包括創(chuàng)建、查詢和xml二、LINQtoXML本節(jié)將介紹LINQtoXML編程的相關(guān)概念,例如如何加載xml、創(chuàng)建全新xml、xml的xml文檔。publicpublicstaticvoid{XElementroot=XElement.Load(path); }Parse方法從一個(gè)字符串加載publicpublicstaticvoid{XElementroot= }2、保存面的示例中調(diào)用XElement對象的Save方法來保存xml文檔在這里就不冗述了面的示例中調(diào)用XElement對象的構(gòu)造函數(shù)來創(chuàng)建xml文檔,在這里就不冗述了。LINQtoXMLxml文檔時(shí),會有代碼縮進(jìn),這使代碼的可讀性大大加強(qiáng)。LINQtoXML在xml樹中遍歷xmlXElementXAttribute類中所提供的方法。ElementsElement方法提供了定位到某個(gè)或某些元素的方式。下面的示例演示了如何遍歷xml樹,并獲取指定元素的方式:publicpublicstaticvoid{XElementroot=newusing(NorthwindDontextdb=newNorthwindD{c=>newXElement,new))}foreach(varitemin{ }}}}5、LINQtoXML一個(gè)重要的特性是能夠方便地修改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進(jìn)行了更新操作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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論