自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linq實現(xiàn)XML轉(zhuǎn)換淺談

開發(fā) 后端
這里介紹Linq實現(xiàn)XML轉(zhuǎn)換,通過 LINQ 查詢,可以輕松地在內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)、SQL 數(shù)據(jù)庫、ADO.NET 數(shù)據(jù)集和XML流或文檔之間轉(zhuǎn)換數(shù)據(jù)。

學(xué)習(xí)Linq時,經(jīng)常會遇到Linq實現(xiàn)XML轉(zhuǎn)換問題,這里將介紹Linq實現(xiàn)XML轉(zhuǎn)換問題的解決方法。

Linq實現(xiàn)XML轉(zhuǎn)換,將內(nèi)存中的對象轉(zhuǎn)換為XML

通過 LINQ 查詢,可以輕松地在內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)、SQL 數(shù)據(jù)庫、ADO.NET 數(shù)據(jù)集和XML流或文檔之間轉(zhuǎn)換數(shù)據(jù)。下面的示例是Linq實現(xiàn)XML轉(zhuǎn)換,將內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)中的對象轉(zhuǎn)換為XML元素。

  1. class XMLTransform  
  2. {  
  3. static void Main()  
  4. {  
  5. // Create the data source by using a collection initializer.  
  6. List<Student> students = new List<Student>()  
  7. {  
  8. new Student {First="Svetlana"Last="Omelchenko"ID=111
    Scores = new List<int>{97, 92, 81, 60}},  
  9. new Student {First="Claire"Last="O’Donnell"ID=112
    Scores = new List<int>{75, 84, 91, 39}},  
  10. new Student {First="Sven"Last="Mortensen"ID=113
    Scores = new List<int>{88, 94, 65, 91}},  
  11. };  
  12.  
  13. // Create the query.  
  14. var studentsToXML = new XElement("Root",  
  15. from student in students  
  16. let x = String.Format("{0},{1},{2},{3}", student.Scores[0],  
  17. student.Scores[1], student.Scores[2], student.Scores[3])  
  18. select new XElement("student",  
  19. new XElement("First", student.First),  
  20. new XElement("Last", student.Last),  
  21. new XElement("Scores", x)  
  22. ) // end "student"  
  23. ); // end "Root"  
  24.  
  25. // Execute the query.  
  26. Console.WriteLine(studentsToXML);  
  27.  
  28. // Keep the console open in debug mode.  
  29. Console.WriteLine("Press any key to exit.");  
  30. Console.ReadKey();  
  31. }  

Linq實現(xiàn)XML轉(zhuǎn)換,此代碼生成下面的XML輸出:

  1. < Root> 
  2.   <student> 
  3.     <First>Svetlana</First> 
  4.     <Last>Omelchenko</Last> 
  5.     <Scores>97,92,81,60</Scores> 
  6.   </student> 
  7.   <student> 
  8.     <First>Claire</First> 
  9.     <Last>O'Donnell</Last> 
  10.     <Scores>75,84,91,39</Scores> 
  11.   </student> 
  12.   <student> 
  13.     <First>Sven</First> 
  14.     <Last>Mortensen</Last> 
  15.     <Scores>88,94,65,91</Scores> 
  16.   </student> 
  17. </Root> 

【編輯推薦】

  1. LINQ查詢操作經(jīng)驗總結(jié)
  2. LINQ遍歷多個數(shù)組深入剖析
  3. Linq查詢Access數(shù)據(jù)文件淺談
  4. LINQ構(gòu)建框架設(shè)計學(xué)習(xí)筆記
  5. LINQ重要組成部分簡介
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-09-14 15:45:28

LINQ刪除XML節(jié)點

2009-06-18 10:07:44

LINQ to ACC

2011-07-22 08:43:08

XML

2009-09-09 15:44:22

Linq DataCo

2009-09-15 16:31:15

LINQ Custom

2009-09-17 13:54:26

LINQ to XML

2009-09-11 11:25:35

LINQ函數(shù)集合

2009-09-10 11:29:00

LINQ to SQL

2009-09-10 15:45:07

Linq使用Selec

2009-09-07 17:32:14

LINQ檢索數(shù)據(jù)

2009-09-08 15:19:52

Linq Where操

2009-09-14 09:49:08

Linq擴展函數(shù)

2009-09-16 11:15:52

Linq聯(lián)接數(shù)據(jù)

2009-01-04 16:58:48

LINQ to XMLLINQXML

2009-09-17 13:30:32

LINQ to XML

2009-09-14 15:12:40

LINQ to XML

2009-09-16 15:33:22

LINQ to XML

2009-09-16 16:52:50

LINQ to XML

2011-07-26 13:58:17

LINQ

2009-09-16 16:59:05

LINQ to XML
點贊
收藏

51CTO技術(shù)棧公眾號