In this post I will discuss how could be read XML file using LINQ. We will see some tips as well.

Let us say we have XML file as below, this file is stored on D drive. This XML contains information about books.

Data.Xml

 

01 <?xml version="1.0" encoding="utf-8" ?>
02 <catalog>
03  <books>
04 <book id="bk101">
05   <author id="1">Gambardella, Matthew</author>
06   <title>XML Developer's Guide</title>
07   <genre>Computer</genre>
08   <price>44.95</price>
09   <publish_date>2000-10-01</publish_date>
10    <description> An in-depth look at creating applications with XML.</description>
11 </book>
12 <book id="bk102">
13   <author id="2">Ralls, Kim</author>
14   <title>Midnight Rain</title>
15   <genre>Fantasy</genre>
16   <price>5.95</price>
17   <publish_date>2000-12-16</publish_date>
18   <description> A former architect battles corporate zombies,an evil sorceress, and her own childhood to become queen of the world.</description>
19 </book>
20 <book id="bk103">
21   <author id="3">Corets, Eva</author>
22   <title>Maeve Ascendant</title>
23   <genre>Fantasy</genre>
24     <price>5.95</price>
25     <publish_date>2000-11-17</publish_date>
26     <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society. </description>
27   </book>
28  
29 <book id="bk104">
30     <author id="4">Corets, Eva</author>
31     <title>Oberon's Legacy</title>
32     <genre>Fantasy</genre>
33     <price>5.95</price>
34     <publish_date>2001-03-10</publish_date>
35     <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>
36  
37   </book>
38  
39 <book id="bk105">
40     <author id="5">Corets, Eva</author>
41     <title>The Sundered Grail</title>
42     <genre>Fantasy</genre>
43     <price>5.95</price>
44     <publish_date>2001-09-10</publish_date>
45     <description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.</description>
46  
47   </book>
48   </books>
49 </catalog>

 

Fetching all the Books

To fetch all the Books, just we need to parse the XML file. Find the descendants book and fetch it in anonymous class.

 

image

 

Fetching a Particular Book

 

If you want to fetch a particular book, we need to apply where condition while parsing XML file.

 

image

 

Fetching Attribute Value of a particular Book

Imagine you need to fetch author Id of a particular book with Id bk102. To do that you need to select as below,

 

image

 

Fetching all the Authors Name only

To fetch the entire author name, we need to execute below query.

 

image

 

For your reference full source code is as below,

Program.cs

 

01 using System;
02 using System.Linq;
03 using System.Xml.Linq;
04  
05 namespace ConsoleApplication23
06 {
07     class Program
08     {
09         static void Main(string[] args)
10         {
11             XDocument document = XDocument.Load("D:\\Data.xml");
12             #region Fetch All the Books
13             var books = from r in document.Descendants("book")
14                         select new
15                             {
16                                 Author = r.Element("author").Value,
17                                 Title = r.Element("title").Value,
18                                 Genere = r.Element("genre").Value,
19                                 Price = r.Element("price").Value,
20                                 PublishDate = r.Element("publish_date").Value,
21                                 Description = r.Element("description").Value,
22  
23                             };
24  
25             foreach (var r in books)
26             {
27                 Console.WriteLine(r.PublishDate + r.Title + r.Author);
28             }
29  
30             Console.ReadKey(true);
31             

Post a Comment

0 Comments