LINQ模型對比全面剖析

iDotNetSpace發表於2009-10-14

這裡主要介紹DOM模型和LINQ模型操作XML的區別,包括介紹LINQ模型上看出XElement的重要性和使用LINQ操作XML。

下面用程式碼對比一下:

  1. //DOM模型  
  2. XmlDocument doc = new XmlDocument();  
  3. XmlElement name = doc.CreateElement("name");  
  4. name.InnerText = "Patrick Hines";  
  5. XmlElement phone1 = doc.CreateElement("phone");  
  6. phone1.SetAttribute("type", "home");  
  7. phone1.InnerText = "206-555-0144";         
  8. XmlElement phone2 = doc.CreateElement("phone");  
  9. phone2.SetAttribute("type", "work");  
  10. phone2.InnerText = "425-555-0145";         
  11. XmlElement street1 = doc.CreateElement("street1");         
  12. street1.InnerText = "123 Main St" 
  13. XmlElement city = doc.CreateElement("city");  
  14. city.InnerText = "Mercer Island";  
  15. XmlElement state = doc.CreateElement("state");  
  16. state.InnerText = "WA";  
  17. XmlElement postal = doc.CreateElement("postal");  
  18. postal.InnerText = "68042";  
  19. XmlElement address = doc.CreateElement("address");  
  20. address.AppendChild(street1);  
  21. address.AppendChild(city);  
  22. address.AppendChild(state);  
  23. address.AppendChild(postal)  
  24. XmlElement contact = doc.CreateElement("contact");  
  25. contact.AppendChild(name);  
  26. contact.AppendChild(phone1);  
  27. contact.AppendChild(phone2);  
  28. contact.AppendChild(address);  
  29. XmlElement contacts = doc.CreateElement("contacts");  
  30. contacts.AppendChild(contact);  
  31. doc.AppendChild(contacts); 

  1. //LINQ模型  
  2. XElement contacts =  
  3. new XElement("contacts",  
  4. new XElement("contact",  
  5. new XElement("name", "Patrick Hines"),  
  6. new XElement("phone", "206-555-0144",  
  7. new XAttribute("type", "home")),  
  8. new XElement("phone", "425-555-0145"  
  9. new XAttribute("type", "work")),  
  10. new XElement("address",  
  11. new XElement("street1", "123 Main St"),  
  12. new XElement("city", "Mercer Island"),  
  13. new XElement("state", "WA"),  
  14. new XElement("postal", "68042")  
  15. )  
  16. )  
  17. ); 

從對比上我們也可以看出LINQ模型的簡單性。我們還可以從LINQ模型上看出XElement的重要性。使用XElement不僅可以從頭建立xml檔案,還可以使用Load的方法從檔案載入。還可以從資料庫中取出所需元素,這就要用到LINQ TO SQL的東西了,同樣可以從陣列中取出元素。操作完成後可以使用Save方法進行儲存。

下面簡單介紹一下增刪查改XML。

  1. //查詢  
  2. foreach (c in contacts.Nodes()) ...{  
  3. Console.WriteLine(c);  

我們看到在輸出XML元素的時候並不需要對每個元素進行強制的型別轉換,這裡C#編譯器已經做了這些事情,它會在輸出的時候呼叫每個元素的ToString()方法。

  1. //插入元素  
  2. XElement mobilePhone = new XElement("phone", "206-555-0168");  
  3. contact.Add(mobilePhone); 

這裡只是很簡單的演示一些操作,至於那些複雜的操作,只要DOM模型能實現的LINQ模型就一定能實現。插入的時候還可以使用AddAfterThis和AddBeforeThis等方法,提高效率。

  1. //刪除元素  
  2. contact.Element("phone").Remove();  
  3. //刪除某一具體元素  
  4. contact.Elements("phone").Remove();  
  5. //刪除一組元素  
  6. contacts.Element(contact").Element("address").RemoveContent();  
  7. //刪除某一元素內容  
  8. //刪除元素還可以使適用SetElement方法,把某一元素設定為null也就是刪除了這元素。  
  9. //修改元素  
  10. contact.Element("phone").ReplaceContent("425-555-0155");  
  11. //這裡是修改第一個phone元素的內容 

當然同樣可以使用SetElement方法,這裡才是它的用武之地。

從上面簡單的介紹我們可以清楚的看到,使用LINQ操作XML是多麼的簡單,這裡使用的C#語法,如果要是使用VB.NET還會更簡單。有一些方法在VB.NET中可以使用但是在C#中卻沒有。畢竟VB.NET是晚繫結語言,可以充分發揮它的優勢。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-616543/,如需轉載,請註明出處,否則將追究法律責任。

相關文章