Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of LINQ Model

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "LINQ model example analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "LINQ model example analysis" bar!

Let's compare it with the code:

/ DOM model XmlDocument doc = new XmlDocument (); XmlElement name = doc.CreateElement ("name"); name.InnerText = "Patrick Hines"; XmlElement phone1 = doc.CreateElement ("phone"); phone1.SetAttribute ("type", "home"); phone1.InnerText = "20655-0144"; XmlElement phone2 = doc.CreateElement ("phone"); phone2.SetAttribute ("type", "work"); phone2.InnerText = "425555-0145" XmlElement street1 = doc.CreateElement ("street1"); street1.InnerText = "123 Main St" XmlElement city = doc.CreateElement ("city"); city.InnerText = "Mercer Island"; XmlElement state = doc.CreateElement ("state"); state.InnerText = "WA"; XmlElement postal = doc.CreateElement ("postal"); postal.InnerText = "68042"; XmlElement address = doc.CreateElement ("address"); address.AppendChild (street1); address.AppendChild (city); address.AppendChild (state) Address.AppendChild (postal) XmlElement contact = doc.CreateElement ("contact"); contact.AppendChild (name); contact.AppendChild (phone1); contact.AppendChild (phone2); contact.AppendChild (address); XmlElement contacts = doc.CreateElement ("contacts"); contacts.AppendChild (contact); doc.AppendChild (contacts) / LINQ model XElement contacts = new XElement ("contacts", new XElement ("contact", new XElement ("name", "Patrick Hines"), new XElement ("phone", "20655-0144", new XAttribute ("type", "home"), new XElement ("phone", "type", "work"), new XElement ("address", new XElement ("street1", "123Main St") New XElement ("city", "Mercer Island"), new XElement ("state", "WA"), new XElement ("postal", "68042"))

We can also see the simplicity of the LINQ model from the comparison. We can also see the importance of XElement from the LINQ model. Using XElement, you can not only create a xml file from scratch, but also load it from a file using the Load method. You can also fetch the desired elements from the database, which is about to use the LINQ TO SQL stuff, as well as the elements from the array. After the operation is completed, you can save it using the Save method.

The following is a brief introduction to add, delete, check and modify XML.

/ / query foreach (c in contacts.Nodes ())... {Console.WriteLine (c);}

We see that there is no need to cast on each element when outputting XML elements, and here the C# compiler has done this, calling the ToString () method of each element on output.

/ / insert element XElement mobilePhone = new XElement ("phone", "206555-0168"); contact.Add (mobilePhone)

Here is just a very simple demonstration of some operations, as for those complex operations, as long as the DOM model can implement the LINQ model can be implemented. Methods such as AddAfterThis and AddBeforeThis can also be used to improve efficiency when inserting.

/ delete element contact.Element ("phone"). Remove (); / delete a specific element contact.Elements ("phone"). Remove (); / delete a set of elements contacts.Element (contact "). Element (" address "). RemoveContent (); / / delete the content of an element / / delete an element can also be applied to the SetElement method, set an element to null, that is, delete the element. / / modify element contact.Element ("phone") .ReplaceContent ("425-555-0155"); / / modify the contents of * phone elements

Of course, you can also use the SetElement method, and this is the opportunity for it to show its talents.

Thank you for your reading, the above is the content of "LINQ model example analysis", after the study of this article, I believe you have a deeper understanding of the LINQ model example analysis of this problem, the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report