Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C#
Article

Search XML records with Operators using LINQ

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
29 Oct 2008CPOL 32.1K   310   11   1
This sample shows how tosearch xml file records using LINQ

Introduction

This article demonstrate how to search the records in the xml file with the help of LINQ by using the operator(>,<,=).

Background

This article is the example of how to use LINQ with Xml

Using the code

Include the System.Xml.Linq name spce in your cs file. Then add two button control in your form. Then down load the sample which i have attached and keep the sample.xml file in c:/ drive. then paste the following code in your cs file.

Code snippet:

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
<?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>

Points of Interest

I learnt how to use LINQ with XML

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Karthikeyan has done B.tech(IT)from Jeppiaar Engineering College,chennai , Tamilnadu, India. Currently working in a S/W MNC as a Senior Software Developer at Chennai for last 5.0 years. He loves to working with Microsoft Technology. He has stared with VB6 then moved to .NET 1.1 and then .NET 2.0 . He is currently working with C#3.0,Asp.NET. He loves Programming in C# . He is having good knowledge on Web Development and Windows Development.

Comments and Discussions

 
QuestionDesign problem Pin
gokceay10-Jan-10 15:53
gokceay10-Jan-10 15:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.