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" ?> |
05 | < author id = "1" >Gambardella, Matthew</ author > |
06 | < title >XML Developer's Guide</ title > |
07 | < genre >Computer</ genre > |
09 | < publish_date >2000-10-01</ publish_date > |
10 | < description > An in-depth look at creating applications with XML.</ description > |
13 | < author id = "2" >Ralls, Kim</ author > |
14 | < title >Midnight Rain</ title > |
15 | < genre >Fantasy</ genre > |
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 > |
21 | < author id = "3" >Corets, Eva</ author > |
22 | < title >Maeve Ascendant</ title > |
23 | < genre >Fantasy</ genre > |
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 > |
30 | < author id = "4" >Corets, Eva</ author > |
31 | < title >Oberon's Legacy</ title > |
32 | < genre >Fantasy</ genre > |
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 > |
40 | < author id = "5" >Corets, Eva</ author > |
41 | < title >The Sundered Grail</ title > |
42 | < genre >Fantasy</ genre > |
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 > |
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.
Fetching a Particular Book
If you want to fetch a particular book, we need to apply where condition while parsing XML file.
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,
Fetching all the Authors Name only
To fetch the entire author name, we need to execute below query.
For your reference full source code is as below,
Program.cs
03 | using System.Xml.Linq; |
05 | namespace ConsoleApplication23 |
09 | static void Main( string [] args) |
11 | XDocument document = XDocument.Load( "D:\\Data.xml" ); |
12 | #region Fetch All the Books |
13 | var books = from r in document.Descendants( "book" ) |
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, |
25 | foreach (var r in books) |
27 | Console.WriteLine(r.PublishDate + r.Title + r.Author); |
30 | Console.ReadKey( true ); |
No comments:
Post a Comment