zoukankan      html  css  js  c++  java
  • 6.0 LINQ & XML

    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-----------------------

  • 相关阅读:
    Linux磁盘管理之创建磁盘分区05
    Linux磁盘管理之设备文件详解04
    Linux命令行上传文件到百度网盘
    Linux磁盘管理之元数据、文件和目录、链接文件03
    std..tr1如何传递引用类型给function
    如何判断一个点是否在矩形之内及C++的操作符重载
    C++11中部分新语法说明
    物理引擎中基于AABB碰撞盒的SAP碰撞检测
    c++动态库中对于字符类型变量的格式化处理
    从tr1中function使用看converting constructor
  • 原文地址:https://www.cnblogs.com/lyli/p/4458689.html
Copyright © 2011-2022 走看看