using System;
using System.Net;
using System.Windows;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.Generic;
namespace LinktoXml.Class
{
/*
*
Linq 语言集合查询,主要用于以下几个方面
1> Linq to Object : 数据源为实现了接口IEnumerable<T> 或IQueryable<T>的内存数据集合
2> Linq to ADO.NET: 数据源为ADO.NET 数据集,这里将数据库中的表结构映射到类结构,并通过ADO.NET从数据库中获取数据集到内存中
通过LINQ进行数据查询
3> Linq to XML : 数据源为XML文档,通过XElement XAttribute等类讲XML文档数据加载到内存中,通过LINQ查询
*
* link select grammer keywords
* from : 指定要查找的数据源以及范围变量
* select: 指定查询要返回的目标数据,可以指定任何类型,甚至是匿名类型
* where : 指定元素的查询条件
* orderby: 指定元素的排序字段和排序方法
* group : 指定元素的分组字段
*/
public class LinkUsage
{
XElement rootE = null;
public LinkUsage()
{
rootE = XElement.Load(@"XMLFile1.xml");
}
/*
* Element() :获取当前XML元素的第一个具有指定名称的子元素
* Elements() :获取当前XML元素的所有子元素,或具有指定名称的所有子元素,
* 返回类型为IEnumerable<XElement>的可用LINQ进行查询的元素集合
* Attribute():获取当前XML元素的具有指定名称的属性
* Attributes():获取当前XML元素的所有属性或者具有指定名称的属性,
* 返回类型为IEnumerable<XAttribute>的LINQ集合
*/
public void SingleSelect()
{
/*查询根节点下的所有"book"元素节点*/
IEnumerable<XElement> query = //query:查询的结构集
from ele in rootE.Elements("book") //ele 表示通过rootE.Elements("book")查询后的一项数据
select ele; //需要查询的数据项,类型等同于结果集的类型
/*依次访问查询结果集*/
String xml = null;
foreach (XElement e in query)
{
xml = xml + e.ToString() + "\n ------- \n";
}
MessageBox.Show(xml);
}
public void queryWhere()
{
/*查询根节点下"book 元素节点,满足属性类型为"edu"的节点*/
IEnumerable<XElement> query =
from ele in rootE.Elements("book")
where ele.Attribute("type").Value == "edu"
select ele;
String xml = null;
foreach(XElement e in query)
{
xml = xml + e.ToString() + "\n ------- \n";
}
MessageBox.Show(xml);
}
public void queryOrderBy()
{
/*查询根节点下"book 元素节点,满足属性类型为"edu"并且价格大于20的元素*/
IEnumerable<XElement> query =
from ele in rootE.Elements("book")
where (ele.Attribute("type").Value == "edu" &&
int.Parse(ele.Element("price").Value) >20)
orderby ele.Attribute("name").Value
select ele;
String xml = null;
foreach(XElement e in query)
{
xml = xml + e.ToString() + "\n ------- \n";
}
MessageBox.Show(xml);
}
public void queryFillToList()
{
var query =
from ele in rootE.Elements("book")
select new { author=ele.Element("author").Value,price=ele.Element("price").Value};
String xml = null;
foreach(var item in query)
{
xml = xml + item.ToString() + "\n ------- \n";
}
MessageBox.Show(xml);
}
}
}
XMLFile.xml 文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<BookList>
<book name="english" type="edu">
<author>zhangweia</author>
<price>10</price>
</book>
<book name="math" type="edu">
<author>zhangweib</author>
<price>20</price>
</book>
<book name="math1" type="edu">
<author>zhangweib</author>
<price>30</price>
</book>
<book name="math2" type="rw">
<author>zhangweib</author>
<price>40</price>
</book>
<book name="math3" type="rw">
<author>zhangweib</author>
<price>50</price>
</book>
<book name="math4" type="rw">
<author>zhangweib</author>
<price>60</price>
</book>
</BookList>
SingleSelect 方法结果如图:
queryWhere 方法结果如图:
queryOrderBy 方法结果如图:
queryFillToList 方法结果如图: