[Original] Introduction to Linq (I): This article will show you the world of linq to xml in minutes, and show you how to use linqxml.

Source: Internet
Author: User

[Original] Introduction to Linq (I): This article will show you the world of linq to xml in minutes, and show you how to use linqxml.

Getting started with Linq (1): takes you to the world of linq to xml in minutes

1. Introduction to Linq

2. Cases of Linq

  • Create a file

  • Add

  • Delete

  • Change

  • Query

1. Introduction to Linq:

  Language Integrated Query is a set of extensions for c # and Visual Basic languages. It allows you to write C # Or Visual Basic code to query memory data in the same way as a database. On the Internet, there are many criticisms or praises for the performance or other aspects of Linq. Here, I don't want to comment on any aspect of Linq, because I always believe that the advantages and disadvantages of Iot products are inevitable. In addition, there are many technologies, such as Linq and multithreading. I think that since these technologies exist, they all have their own principles. Determine whether to introduce these technologies based on the needs of your project. Technology itself is born to serve projects.

This blog will take you through a few simple and traditional examples of adding, deleting, modifying, and querying to fully enjoy the fun of linq.

 

2. Cases of Linq

 

Create a file

  

1 // create a file path 2 string path = string. format (@ "F :\{ 0 }. xml ", DateTime. now. toString ("yyyyMMddhhmmss"); 3 FileInfo fiXML = new FileInfo (path); 4 // if the file does not exist 5 if (! (FiXML. exists) 6 {7 // create xml Document 8 XDocument xelLog = new XDocument (9 new XDeclaration ("1.0", "UTF-8", "no "), 10 new XElement ("ipmsg", 11 new XElement ("msg_log", 12 new XElement ("user", "monkey"), 13 new XElement ("logdate", DateTime. now. toString ("yyyyMMddhhmmss"), 14 new XElement ("message", "") 15) 16) 17); 18 xelLog. save (path); 19}

 

The xml file after running should be like this:

  

1 <? Xml version = "1.0" encoding = "UTF-8" standalone = "no"?> 2 <ipmsg> 3 <msg_log> 4 <user> monkey brother </user> 5 <logdate> 20150421024045 </logdate> 6 <message> monkey brother traveled here </message> 7 </msg_log> 8 </ipmsg>

 

Add

1 string path = @ "F: \ 20150421024045.xml"; 2 // check whether the File exists 3 if (File. exists (path) 4 {5 // instantiate XMLog 6 XElement xelem = XElement. load (path); 7 8 // execute the linq add (xmlLOG) 9 XElement newLog = new XElement ("msg_log", 10 new XElement ("user", "sashan "), 11 new XElement ("logdate", DateTime. utcNow. toString (), 12 new XElement ("message", ", ") 13); 14 xelem. add (newLog); 15 // save xml16 xelem. save (path); 17 18}

The xml file after running should be like this:

  

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <ipmsg> 3 <msg_log> 4 <user> monkey brother </user> 5 <logdate> 20150421024045 </logdate> 6 <message> monkey brother traveled here </message> 7 </msg_log> 8 <msg_log> 9 <user> Sha Seng </user> 10 <logdate> 4/21/2015 6:49:03 AM </logdate> 11 <message>, the master was captured by monsters </message> 12 </msg_log> 13 </ipmsg>

Delete

 

1 string path = @ "F: \ 20150421024045.xml"; 2 // check whether the File exists 3 if (File. exists (path) 4 {5 // instantiate XMLog 6 XElement xelem = XElement. load (path); 7 8 var queryXML = from xmlLog in xelem. descendants ("msg_log") 9 where xmlLog. element ("user "). value = "monkey" 10 select xmlLog; 11 12 queryXML. remove (); 13 14 // save xml15 xelem. save (path); 16 17}

 

The xml file after running should be like this:

 

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <ipmsg> 3 <msg_log> 4 <user> Sha Seng </user> 5 <logdate> 4/21/2015 6:49:03 AM </logdate> 6 <message>, the master was captured by monsters </message> 7 </msg_log> 8 </ipmsg>

 

Change

1 string path = @ "F: \ 20150421024045.xml"; 2 // check whether the File exists 3 if (File. exists (path) 4 {5 // instantiate XMLog 6 XElement xelem = XElement. load (path); 7 8 var queryXML = from xmlLog in xelem. descendants ("msg_log") 9 where xmlLog. element ("user "). value = "Sha Seng" 10 select xmlLog; 11 queryXML. firstOrDefault (). element ("message "). value = "second brother, Master brother captured by monsters"; 12 13 // save xml14 xelem. save (path); 15 16}

 

The xml file after running should be like this:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <ipmsg> 3 <msg_log> 4 <user> Sha Seng </user> 5 <logdate> 4/21/2015 6:49:03 AM </logdate> 6 <message> Senior Engineer 2, </message> 7 </msg_log> 8 </ipmsg>

Query

 

1 string path = @ "F: \ 20150421024045.xml"; 2 string messageByShaSeng = string. empty; 3 // check whether the File exists 4 if (File. exists (path) 5 {6 // instantiate XMLog 7 XElement xelem = XElement. load (path); 8 var queryXml = from xmlLog in xelem. descendants ("msg_log") 9 // All records named Bin 10 where xmlLog. element ("user "). value = "Sha Seng" 11 12 select xmlLog; 13 messageByShaSeng = queryXml. firstOrDefault (). element ("message "). value; 14} 15 16 Console. writeLine (messageByShaSeng );

 

The input content is: Brother 2, Master, captured by monsters

 

 

  All content in this article is original. You are welcome to read and explore it. Please indicate the source for reprinting. Otherwise, you will be held legally responsible.(Although linq is already an example, it is also original)

We hereby declare that all comments and private messages will be replied immediately. You are also welcome to correct the mistakes and make progress together. In addition, if you want to learn more about Linq, you may wish to take a look at it here.

 

  If you read this blog. This is helpful to you. Please do not mean your "Recommendations". Your recommendations will be my greatest motivation. If you have any questions, you can comment on them.

 

 

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.