新聞中心
這篇文章主要講解了“LINQ To XML怎么使用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“LINQ To XML怎么使用”吧!
創(chuàng)新互聯(lián)建站成都企業(yè)網(wǎng)站建設服務,提供網(wǎng)站設計制作、成都網(wǎng)站建設網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設計,成都響應式網(wǎng)站建設公司,網(wǎng)頁設計師打造企業(yè)風格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務。歡迎咨詢做網(wǎng)站需要多少錢:18980820575
LINQ To XML——XML操作
XML數(shù)據(jù)越來越廣泛地應用在各種實際的開發(fā)系統(tǒng)中,為了簡化對XML數(shù)據(jù)的開發(fā)和利用,微軟的開發(fā)團隊開發(fā)了這個全新的LINQ to XML框架。首先,LINQ to XML框架是一個輕量級的XML編程API,開發(fā)者利用該框架,幾乎可以取代原有的XML數(shù)據(jù)開發(fā)方式,非常簡單地創(chuàng)建、讀取并操作內存中的XML數(shù)據(jù),如利用函數(shù)構造方法創(chuàng)建XML樹等。其次,LINQ to XML框架中集成了LINQ的強大功能,開發(fā)者可以采用一致的編程方式,非常容易地操作并查詢XML數(shù)據(jù)。
XElement Students = new XElement("Students", new XElement("Student", new XElement("Name", "張三"), new XElement("Sex", "男"), new XElement("Age", 20)), new XElement("Student", new XElement("Name", "李四"), new XElement("Sex", "女"), new XElement("Age", 19)) ); Console.WriteLine(Students); 編歷XML XElement Students = new XElement("Students", new XElement("Student", new XElement("Name", "張三"), new XElement("Sex", "男"), new XElement("Age", 20)), new XElement("Student", new XElement("Name", "李四"), new XElement("Sex", "女"), new XElement("Age", 19)) ); foreach (XNode node in Students.Nodes()) { Console.WriteLine(node); Console.WriteLine("----------------------------"); } foreach (XElement ele in Students.Elements()) { Console.WriteLine(ele); Console.WriteLine("********************************"); }
添加XML節(jié)點
XElement Students = new XElement("Students", new XElement("Student", new XElement("Name", "張三"), new XElement("Sex", "男"), new XElement("Age", 20)), new XElement("Student", new XElement("Name", "李四"), new XElement("Sex", "女"), new XElement("Age", 19)) ); foreach (XElement ele in Students.Elements()) { ele.Element("Age").AddAfterSelf(new XElement("Hight", 173)); ele.Element("Age").AddBeforeSelf(new XElement("Weight", 73)); ele.Add (new XElement("Hobby", "Coding")); } Console.WriteLine(Students) 更新XML節(jié)點 XElement Students = new XElement("Students", new XElement("Student", new XElement("Name", "張三"), new XElement("Sex", "男"), new XElement("Age",new XAttribute ("Year",1989/8/22), 20)) ); Students.Element(“Student”).Element(“Age”).ReplaceWith(new XElement(“Age”, 28));//替換掉整個節(jié)點 // Students.Element(“Student”).Element(“Age”).ReplaceNodes ( 28);//只替換節(jié)點值 // Students.Element(“Student”).Element(“Age”).ReplaceAll (28);//替換掉整個節(jié)點 Console.WriteLine(Students); 刪除XML節(jié)點 XElement Students = new XElement("Students", new XElement("Student", new XElement("Name", "張三"), new XElement("Sex", "男"), new XElement("Age",new XAttribute ("Year","1989/8/22"), 20)) ); //Students.Element("Student").Element("Age").Remove ();//移除節(jié)點 //Students.Element("Student").Element("Age").RemoveAll();//移除節(jié)點的值和屬性 Students.Element("Student").Element("Age").RemoveNodes();//移除節(jié)點的值 Console.WriteLine(Students); 添加XML屬性 XElement Students = new XElement("Students", new XElement("Student", new XElement("Name", "張三"), new XElement("Sex", "男"), new XElement("Age",new XAttribute ("Year","1989/8/22"), 20)) ); Students.Element("Student").SetAttributeValue("dd","dddd"); Console.WriteLine(Students); 更新XML屬性 Students.Element("Student").Element("Age").ReplaceAttributes(new XAttribute("Year","dd")); Students.Element("Student").Element("Age").SetAttributeValue("Year", "dddd"); 刪除XML屬性 Students.Element("Student").Element("Age").Attribute("Year").Remove (); Students.Element("Student").Element("Age").RemoveAttributes (); 遍歷XML屬性 var Attr = from att in Students.Element("Student").Element("Age").Attributes() select att; foreach (var att in Attr) { Console.WriteLine(att); }
感謝各位的閱讀,以上就是“LINQ To XML怎么使用”的內容了,經過本文的學習后,相信大家對LINQ To XML怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!
網(wǎng)站題目:LINQToXML怎么使用
標題URL:http://www.ef60e0e.cn/article/pidcii.html