Linq to Xml 学习笔记一

   通过Linq进行数据查询
linq to xml :数据源为xml文档,通过XElement Xattribute 等类讲XML 文档数据加载到内存中,通过LINQ查询
    link select grammer keywords

from  :   指定要查找的数据源以及范围变量
select:   指定查询要返回的目标数据,可以指定任何类型,甚至是匿名类型
where :   指定元素的查询条件
orderby : 指定元素的排序字段和排序方法
group   : 指定元素的分组字段

Element()  :获取当前XML元素的第一个具有指定名称的子元素
Elements() :获取当前XML元素的所有子元素,或具有指定名称的所有子元素,
            返回类型为IEnumerable<XElement>的可用LINQ进行查询的元素集合

Attribute():  获取当前XML元素的具有指定名称的属性
Attributes(): 获取当前XML元素的所有属性或者具有指定名称的属性,
              返回类型为IEnumerable<XAttribute>的LINQ集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace LinqToXml
{
    public class Link
    {

        XElement root = null;

        public Link()
        {
            root = XElement.Load(@"like.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 root.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);
            Console.WriteLine(xml);

        }

        public void queryWhere()
        {
            /*查询根节点下"book 元素节点,满足属性类型为"edu"的节点*/
            IEnumerable<XElement> query =
                                        from ele in root.Elements("book")
                                        where ele.Attribute("type").Value == "edu"  //查询属性名为 edu 
                                        select ele;
            String xml = null;
            foreach (XElement e in query)
            {
                xml = xml + e.ToString() + "\n -------  \n";
            }

            Console.WriteLine(xml);
        }

        public void queryOrderBy()
        {
            /*查询根节点下"book 元素节点,满足属性类型为"edu"并且价格大于20的元素*/
            IEnumerable<XElement> query =
                                        from ele in root.Elements("book")
                                        where (ele.Attribute("type").Value == "edu" &&     //循环book节点
                                              int.Parse(ele.Element("price").Value) > 20)  //book 元素节点 大于20的元素
                                        orderby ele.Attribute("name").Value                //按name 排序 
                                        select ele;                                        //返回IEnumerable<XElement> 集合
            String xml = null;
            foreach (XElement e in query)
            {
                xml = xml + e.ToString() + "\n -------  \n";
            }

            Console.WriteLine(xml);
        }


        public void queryFillToList()
        {
            var query =
                        from ele in root.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";
            }
            Console.WriteLine(xml);
        }
    }
}

posted @ 2011-04-07 12:06  jackyong  阅读(594)  评论(2编辑  收藏  举报