码迷,mamicode.com
首页 > 其他好文 > 详细

6.0 LINQ & XML

时间:2015-04-27 14:46:50      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

To be honest, after I finished the teacher‘s lecture, I didn‘t quite understand the two topics (maybe due to the fast speed of teaching or something else). So I searched for some simple corresponding codes to get further understanding.

Following are something that I tried.

--------LINQ--------

part.1

技术分享

The first part is the definition of the Customer, with three class member variables FirstName, LastName, EmailAddress, and an overrided member function ToString() to show the whole info of a customer.

技术分享

The second part is about the function CreateCustomerList(), which constructs a list of customers with initial values. As we can see, there are five customers with different names and email address seperately.

技术分享

The two parts above are in fact preparation for LINQ implementation. And this last part in Main() shows how LINQ works. 

After creating a list of customers, here comes LINQ. Those several lines mean to select the customer from the list whose FirstName is "Donna" and then to store the record into the result.

Then, make a subtle change to the content of the list, so that there are two customers whose FirstName is "Donna". And the result of the selection is changed automatically along with the change of the list, as we can verify in the following output.

技术分享

--------XML--------

This time, I still used the first two parts of codes above to define a class of a customer and a list of customers. Then I made a change in Main() function.

技术分享

First, create an xmlDocument and add an xmlElement as the rootelement of the document.

Then, in the foreach loop, create a customerElement for each customer of the list. To hold their info, we have another three xmlElements appended to the customerElement. To fill the content of each element, we can use InnerText. At the end of the loop, it‘s necessary to append each customerElement to the root.

As for the output, to see the structure of the xmlDocument more clearly, I put it into an .txt file, and then post it as following.

<Customers>
    <Customer>
        <FirstName>Orlando</FirstName>
        <LastName>Gee</LastName>
        <EmailAddress>orlando0@adventure-works.com</EmailAddress>
    </Customer>
    <Customer>
        <FirstName>Keith</FirstName>
        <LastName>Harris</LastName>
        <EmailAddress>keith0@adventure-works.com</EmailAddress>
    </Customer>
    <Customer>
        <FirstName>Donna</FirstName>
        <LastName>Carreras</LastName>
        <EmailAddress>donna0@adventure-works.com</EmailAddress>
    </Customer>
    <Customer>
        <FirstName>Janet</FirstName>
        <LastName>Gates</LastName>
        <EmailAddress>janet0@adventure-works.com</EmailAddress>
    </Customer>
    <Customer>
        <FirstName>Lucy</FirstName>
        <LastName>Harrington</LastName>
        <EmailAddress>lucy0@adventure-works.com</EmailAddress>
    </Customer>
</Customers>
请按任意键继续. . .

So,,, that‘s all.

--------------------END & TO BE CONTINUED-----------------------

6.0 LINQ & XML

标签:

原文地址:http://www.cnblogs.com/lyli/p/4458689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!