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>[email protected]</EmailAddress>
    </Customer>
    <Customer>
        <FirstName>Keith</FirstName>
        <LastName>Harris</LastName>
        <EmailAddress>[email protected]</EmailAddress>
    </Customer>
    <Customer>
        <FirstName>Donna</FirstName>
        <LastName>Carreras</LastName>
        <EmailAddress>[email protected]</EmailAddress>
    </Customer>
    <Customer>
        <FirstName>Janet</FirstName>
        <LastName>Gates</LastName>
        <EmailAddress>[email protected]rks.com</EmailAddress>
    </Customer>
    <Customer>
        <FirstName>Lucy</FirstName>
        <LastName>Harrington</LastName>
        <EmailAddress>[email protected]</EmailAddress>
    </Customer>
</Customers>
请按任意键继续. . .

So,,, that‘s all.

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

时间: 2024-11-05 16:08:42

6.0 LINQ & XML的相关文章

【IOS】AFNetworking 2.0中XML请求处理专题

AFNetworking 2.0中XML请求处理专题 1.1     XML请求处理 1.1.1  xml处理机制 因为AFNetworking2.0中,对于响应返回的xml格式没有做专门的解析处理,因此需要开发者自己来做处理.在笔者的项目中,使用了一个叫AFGDataXMLRequestOperation的第三方类库来统一处理. 该库的xml解析逻辑基于GDataXML-HTML类库.不过类库处理逻辑应该还是基于AFNetworking 1.0版本,有两处需要做适配修改,不然调试不过: 1.在

Android 5.0 system_fonts.xml文件的解析过程

Android 5.0 system_fonts.xml文件的解析过程 首先看看看5.0 中familyset version="22" 的格式 20 <family name="sans-serif"> 21 <font weight="100" style="normal">Roboto-Thin.ttf</font> 22 <font weight="100"

Tomcat7.0+ web.xml问题

Tomcat7+版本的web.xml都加上 <context-param> <param-name>webAppRootKey</param-name> <param-value>项目名(唯一)</param-value> </context-param> Tomcat7.0+ web.xml问题

C# LINQ &amp; XML

LINQ(Language-INtegrated Query) 是一种用作查找.存取数据库或XML文件的语言模式. 是沟通面向对象语言与数据库的方式.与SQL很相似. using System; using System.Collections.Generic; using System.Linq; //linq namespace namespace Programming_CSharp { // Simple customer class public class Customer //定义

tomcat7.0 server.xml修改(默认目录,端口,多个域名)

tomcat目录下的conf文件夹, server.xml 1.修改默认webapps 1 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> 2 ... 3 </Host> appBase 可以修改成你的站点位置如:D:/web 2.修改默认目录ROOT 具体是是在<Host></

spring3.0定时器 xml配置和注解方式

1.xml配置方式 web.xml <!-- 配置spring监听器和配置文件路径 -->     <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>classpath:applicationContext.xml</param-value>     </context-param>    

C# 3.0 LINQ的准备工作

局部变量 隐式类型允许你用var修饰类型.用var修饰只是编译器方便我们进行编码,类型本身仍然是强类型的,所以当编译器无法推断出类型时(例如你初始化一个变量却没有为其赋值,或赋予null,此时就无法推断它的类型),用var修饰就会发生错误.另外,只能对局部变量使用隐式类型. 使用隐式类型的几个时机: 当变量的类型太长或者难以推测,但类型本身不重要时,比如你的LINQ语句中用了Groupby,那么一般来说基本很少人可以准确地推测出结果的类型吧... 当变量初始化时,此时可以根据new后面的类型得知

[ActionScript 3.0] 处理xml内容换行时行间距较大问题的一种简单方法

我们一定遇到过这种情况,在读取xml里的文章内容时,一旦有换行的位置在flash里显示出来的行间距会比较大,而并非我们想要的效果,解决这个问题的方法除了使用正则表达式以外,这里介绍一种比较简单的方法,利用str.split("\r\n").join("\r")方法即可,如下示例: 1 package 2 { 3 import flash.display.Sprite; 4 import flash.text.TextField; 5 import flash.tex

[ActionScript 3.0] AS3.0 生成xml方法之一

var type:Array = ["type0", "type1", "type2"]; var property:Array = [["20","0","1.jpg"], ["60","0","2.jpg"], ["120","0","3.jpg"]];var da