本文只总结linq入门和读写XML,本文属于笔记。
引用:using System.Linq;
ling主要包含4个组件:
Linq to Objects——这个组件可以查询集合数据,如数组和List。
Linq to XML
Linq to DataSet
Linq to SQL
ling特性
隐式类型局部变量 :var
对象初始化器 : new p {x=1,y="2"}
Lambda表达式: =>
扩展方法 :this
匿名类型:
LINQ 隐式类型局部变量
可以用var 声明大部分类型的变量,前提是变量必须赋值。
var name = "Olive"; 等同于 string name= "Olive";
var age = 22; 等同于 int age = 22;
var Frieds = new[] { "A", "B", "C" }; 等同于 string[] Frieds = new string[] { "A", "B", "C" };
var numbers=new[]{1,2,3}; 等同于int[] numbers=new int[]{1,2,3};
var process=new ProcessData(); 等同于ProcessData process=new ProcessData();
对象和集合初始化器
1.对象初始化
var data=new p{Id=1,Name=“111”}; 括号内直接初始化类
2.集合初始化
var num=new List<int>{0,1,2,3,4}; 等同于以前的 num.Add(0) ;num.Add(1)........
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] words = {"aa", "bbbb", "ccccc", "dddddd", "eeeeeeee"};
//Get only short words
var shortWords = from word in words
where word.Length <= 5
select word;
//Print each word out
foreach (var word in shortWords)
{
Console.WriteLine(word);
}
Console.ReadLine();
}
}
对象实现了IEnumerable<T>或IQueryable的<T>泛型接口的数据源,T可以是int等。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Operators
{
class LINQQueryExpressions
{
static void Main()
{
// Specify the data source.
int[] scores = new int[] { 1, 111, 22, 2222 };
// Define the query expression.
IEnumerable<int> scoreQuery = from score in scores
where score > 80
select score;
// Execute the query.
foreach (int i in scoreQuery)
{
Console.Write(i + " ");
}
Console.ReadLine();
}
}
}
linq to XML 取代以往DOM命令方式,改为函数式创建节点。
1.函数式创建XML
XElement books = new XElement("book", //创建节点
new XElement("title","书名"), //在节点内又创建节点1
new XElement("author","作者"), //在节点内创建节点2
new XElement("name",userName), // 内容可以是变量
new XElement("name",GetUserName()) //内容可以是方法返回值
);
结果
<book>
<title>书名</title>
<author>作者</author>
</book>
2.加载XML
XElement x=XElement.load(@"c:\boos.xml");
XElement x=XElement.load("http://www.???.com/rss.xml");
3.添加节点Add方法
XElement books = new XElement("book");
books.Add(new XElement("id","1"));
使用另一个xml的book节点内容添加节点
XElement b=XElement.Load("b.xml");
XElement book=new XElement("book");
book.Add(b.Elements("book"));
以上添加都是从XML文档最后开始,或属性的最后开始添加。
book.AddAfterSelf();从某个元素第二个添加
book.AddBeforeSelf();从某个元素前面添加
4.移除节点Remove()方法
books.Element("book").Remove();//移除第一个book
books.Elements("book").Remove();//移除所有book
books.Elements("book").Element("book").Value;//移除节点的内容,但保留节点标签
5.更新节点内容SetElementValue() ,ReplaceNodes()
Element books=new Element("b.xml");
books.Element("book").SetElementValue("author","新的内容");//简单更改一个
books.Element("book").ReplaceNodes( //批量更改
new XElement("title","新书名"),
new XElement("author","新作者"),
);
6.添加属性 SetAttributeValue()
new XElement("book", new XAttribute("date","1,2018"));
或 book.Add(new XAttribute("date","1,2018"));添加属性
book.SetAttributeValue("date","1,2019");更改属性
book.Attribute("date").Remove();删除
7.保存XML books.Save(@"c:\books.xml");
8.查询XML
XML实例
<books name="abc">
<book>a</book>
<book>b</book>
<books/>
1.Element()方法 得到单一的标签下的所有内容,如果有多个相同标签,只查询第一个
XElement b=XElement.Load("b.xml");
XElement books=b.Element("books");
Console.WriteLine(books); 返回整个XML文档
2.Elements()方法 得到指定父标签下的所有想查询的标签,小范围查询方法
IENUMERABLE<XElement> books=books.Elements("book"); 必须要使用了Element()后,才能用Elements()
foreach(XElement book in books) 由于Elements()方法返回的是IEnumerable<T>类型,所以要使用foreach遍历
{
Console.WriteLine((string)book);
}
3.Attribute() 方法 取属性值
XAttribute name=books.Attribute("name");
Console.WriteLine((string)name);返回abc
4.Descendants() 全范围查询,无需先使用Element()
接1程序 foreach(XElement book in b.Descendants("book"))
{
Console.WriteLine((string)book);
}