C#学习笔记:linq和xml

linq语言是关系型数据库和面向对象语言之间的桥梁性的接口

--------------------------linq的查询操作---------------------------------------------

1.获取数据源

2.创建查询

3.执行查询

using System.Linq;
using System;
class IntroToLINQ
{
    static void Main()
    {
        // LinQ查询的三个不能分:
        //  1. 设置数据源.
        int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

        // 2. 查询操作.
        // numQuery 是一个泛型接口 IEnumerable<int>
        var numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;

        // 3. 执行查询操作.
        foreach (int num in numQuery)
        {
            Console.WriteLine("{0} ", num );
        }
        Console.Read();
    }
}

-------------------------linq的查询数据源---------------------------------------------

1.当数据源是数组时,它隐式支持泛型 IEnumerable<T> 接口,所以可以直接进行查询

2.当数据源是xml文件时,不可以直接查询,而是需要通过转化成XElement

3.查询sql数据库同样需要转化

Northwnd db = new Northwnd(@"c:文件名.mdf");

// 数据库查询
IQueryable<Customer> custQuery =
    from cust in db.Customers
    where cust.City == "A"
    select cust;

该查询表达式包含三个子句:from、where 和 select。from 子句指定数据源,where 子句应用筛选器,select 子句指定返回的元素的类型。

----------------------Linq的查询的执行--------------------------------------------------

1.延迟查询

先将查询的内容的放在一个查询变量

然后利用foreach进行查徇

for ( num in numList )
{
    //对数据进行的操作

}

2.立即查询

对一系列源元素执行聚合函数的查询必须首先循环访问这些元素。 CountMaxAverageFirst 就属于此类查询。 由于查询本身必须使用 foreach 以便返回结果,因此这些查询在执行时不使用显式 foreach 语句。 另外还要注意,这些类型的查询返回单个值,而不是 IEnumerable 集合。 

-----------------------xml文件--------------------------------------------------------

<?xml version="1.0" encoding="gb2312"?>
<bookstore>
  <book genre="fantasy" ISBN="2-3631-4">
    <title>Oberon's Legacy</title>
    <author>Corets, Eva</author>
    <price>5.95</price>
  </book>
</bookstore>

1、往<bookstore>节点中插入一个<book>节点:

(1)首先打开xml文件

 XmlDocument xmlDoc=new XmlDocument();
   xmlDoc.Load("bookstore.xml");

(2)找到bookstore这一节点

 XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>

(3)创建book节点,并设置它的属性

xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
   xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性

(4)将创建的节点添加到book节点上,并存储文件

root.AppendChild(xe1);//添加到<bookstore>节点中
   xmlDoc.Save("bookstore.xml");


2.修改节点(找到节点后遍历其子节点,找到指定节点删掉即可

  XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
   foreach(XmlNode xn in nodeList)//遍历所有子节点
   {
    XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
    if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
    {
     xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
 
     XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
     foreach(XmlNode xn1 in nls)//遍历
     {
      XmlElement xe2=(XmlElement)xn1;//转换类型
      if(xe2.Name=="author")//如果找到
      {
       xe2.InnerText="亚胜";//则修改
       break;//找到退出来就可以了
      }
     }
     break;
    }
   }

3.删除节点

与修改类似,通过遍历找到节点后利用removeall()函数删除

XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
 
   foreach(XmlNode xn in xnl)
   {
    XmlElement xe=(XmlElement)xn;
    if(xe.GetAttribute("genre")=="fantasy")
    {
     xe.RemoveAttribute("genre");//删除genre属性
    }
    else if(xe.GetAttribute("genre")=="update李赞红")
    {
     xe.RemoveAll();//删除该节点的全部内容
    }
   }
   xmlDoc.Save("bookstore.xml");
原文地址:https://www.cnblogs.com/tjullin-251249/p/4460854.html