使用 LINQ 的运算符搜索 XML 记录





1.00/5 (1投票)
此示例展示了如何使用 LINQ 搜索 xml 文件记录。
引言
本文演示了如何使用 LINQ 运算符(>、<、=)在 XML 文件中搜索记录。
背景
本文是 LINQ 与 XML 结合使用的示例。
使用代码
在您的 cs 文件中包含 System.Xml.Linq 命名空间。然后在您的窗体中添加两个按钮控件。然后下载我附加的示例,并将 sample.xml 文件保存在 c:/ 驱动器中。然后将以下代码粘贴到您的 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.Linq; namespace LINQSAMPLE { public partial class LINQwithXML : Form { public LINQwithXML() { InitializeComponent(); } /// <summary> /// search xml record using "=" operator /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { XDocument doc = XDocument.Load("C:/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)); } } /// <summary> /// search xml record using "> and <" operator /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { XDocument doc = XDocument.Load("C:/sample.xml"); var records = from book in doc.Root.Elements("book") where (int)book.Element("id") > 101 && (int)book.Element("id") < 104 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>