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

  • 相关阅读:
    详解ASP.NET中获取小程序二维码图片的操作<后端>
    ASP.NET 后台上传图片
    使用Js在前台画二维码
    在网页上点击图片打开一个新页面显示大图
    C# 使用RabbitMQ消息队列
    Git如何拉取指定远程分支
    win10专业版激活方法
    基于.Net Core3.1 MVC + EF Core的项目(一)框架的初步搭建
    session未过期就丢失的原因以及处理方式
    DES加密和base64加密
  • 原文地址:https://www.cnblogs.com/lyli/p/4458689.html
Copyright © 2011-2022 走看看