使用LINQ查询XML记录

原文地址及代码附件:http://www.dingos.cn/index.php?topic=1967.0

介绍

这篇文章演示在LINQ中使用运算符(><=)查询xml中的记录。

背景

这篇文章是使用LINQ操作Xml的示例。

使用代码

cs文件中要引用System.Xml.Linq命名控件。并添加两个按钮在窗体中。下载这个例子,sample.cs文件在编译后可执行文件的目录下。cs代码:

代码片段:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.Xml.Linq;

 

namespace LINQSAMPLE {

    public partial class LINQwithXML : Form {

        public LINQwithXML() {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e) {

            XDocument doc = XDocument.Load("sample.xml");

 

            var records =

                from book in doc.Root.Elements("book")

                where (string)book.Element("id") == "101"

                select book;

 

            foreach(var book in records) {

                MessageBox.Show(

                    string.Format("Author: {0}/r/nTitle: {1}/r/nPrice: {2}",

                    book.Element("author").Value,

                    book.Element("title").Value,

                    book.Element("price").Value));

            }

        }

 

        private void button2_Click(object sender, EventArgs e) {

            XDocument doc = XDocument.Load("sample.xml");

 

            var records =

                from book in doc.Root.Elements("book")

                where (int)book.Element("id") > 101 &&

                                 (int)book.Element("id") < 110

                select book;

 

            foreach(var book in records) {

                MessageBox.Show(

                    string.Format("Author: {0}/r/nTitle: {1}/r/nPrice: {2}",

                    book.Element("author").Value,

                    book.Element("title").Value,

                    book.Element("price").Value));

            }

        }

    }

}

Sample.Xml

<?xml version="1.0"?>

<catalog>

    <book>

        <id>101</id>

        <author>karthikeyan</author>

        <title>XML Developer's Guide</title>

        <genre>Computer</genre>

        <price>44.95</price>

        <publish_date>2008-10-01</publish_date>

        <description>An in-depth look at creating applications

        with XML.</description>

    </book>

    <book>

        <id>102</id>

        <author>Smithi</author>

        <title>Midnight Rain</title>

        <genre>Fantasy</genre>

        <price>5.95</price>

        <publish_date>2008-02-16</publish_date>

        <description>A former architect battles corporate zombies,

        an evil sorceress, and her own childhood to become queen

        of the world.</description>

    </book>

    <book>

        <id>103</id>

        <author>Anni</author>

        <title>Maeve Ascendant</title>

        <genre>Fantasy</genre>

        <price>5.95</price>

        <publish_date>2007-11-17</publish_date>

        <description>After the collapse of a nanotechnology

        society in England, the young survivors lay the

        foundation for a new society.</description>

    </book>

    <book>

        <id>104</id>

        <author>Ellora</author>

        <title>Oberon's Legacy</title>

        <genre>Fantasy</genre>

        <price>5.95</price>

        <publish_date>2007-03-10</publish_date>

        <description>In post-apocalypse England, the mysterious

        agent known only as Oberon helps to create a new life

        for the inhabitants of London. Sequel to Maeve

        Ascendant.</description>

    </book>

    <book>

        <id>105</id>

        <author>Becham</author>

        <title>The Sundered Grail</title>

        <genre>Fantasy</genre>

        <price>5.95</price>

        <publish_date>2007-09-10</publish_date>

        <description>The two daughters of Maeve, half-sisters,

        battle one another for control of England. Sequel to

        Oberon's Legacy.</description>

    </book>

    <book>

        <id>106</id>

        <author>Runy</author>

        <title>Lover Birds</title>

        <genre>Romance</genre>

        <price>4.95</price>

        <publish_date>2007-09-02</publish_date>

        <description>When Carla meets Paul at an ornithology

        conference, tempers fly as feathers get ruffled.</description>

    </book>

    <book>

        <id>107</id>

        <author>Ronoldo</author>

        <title>Splish Splash</title>

        <genre>Romance</genre>

        <price>4.95</price>

        <publish_date>2007-11-02</publish_date>

        <description>A deep sea diver finds true love twenty

        thousand leagues beneath the sea.</description>

    </book>

    <book>

        <id>108</id>

        <author>Ronoldeno</author>

        <title>Creepy Crawlies</title>

        <genre>Horror</genre>

        <price>4.95</price>

        <publish_date>2007-12-06</publish_date>

        <description>An anthology of horror stories about roaches,

        centipedes, scorpions  and other insects.</description>

    </book>

    <book>

        <id>109</id>

        <author>Maradona</author>

        <title>Paradox Lost</title>

        <genre>Science Fiction</genre>

        <price>6.95</price>

        <publish_date>2006-11-02</publish_date>

        <description>After an inadvertant trip through a Heisenberg

        Uncertainty Device, James Salway discovers the problems

        of being quantum.</description>

    </book>

    <book>

        <id>110</id>

        <author>Sachin</author>

        <title>Microsoft .NET: The Programming Bible</title>

        <genre>Computer</genre>

        <price>36.95</price>

        <publish_date>2008-02-09</publish_date>

        <description>Microsoft's .NET initiative is explored in

        detail in this deep programmer's reference.</description>

    </book>

    <book>

        <id>111</id>

        <author>Bruno</author>

        <title>MSXML3: A Comprehensive Guide</title>

        <genre>Computer</genre>

        <price>36.95</price>

        <publish_date>2006-12-01</publish_date>

        <description>The Microsoft MSXML3 parser is covered in

        detail, with attention to XML DOM interfaces, XSLT processing,

        SAX and more.</description>

    </book>

    <book>

        <id>112</id>

        <author>Mendhak</author>

        <title>Visual Studio 7: A Comprehensive Guide</title>

        <genre>Computer</genre>

        <price>49.95</price>

        <publish_date>2007-04-16</publish_date>

        <description>Microsoft Visual Studio 7 is explored in depth,

        looking at how Visual Basic, Visual C++, C#, and ASP+ are

        integrated into a comprehensive development

        environment.</description>

    </book>

</catalog>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜晚回家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值