Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Tip/Trick

Reading XML using LINQ

Rate me:
Please Sign up or sign in to vote.
4.22/5 (5 votes)
5 Feb 2013CPOL 76.1K   14   4
This article will help you understand the power of LINQ.

Introduction

We have different ways of reading XML, i.e., legacy approach of using a DOM object to read XML or other approaches.

Background

Let us assume that we have a custom created XML that has a series of tags and we want to go through the XML and get the value for further processing.

Using the code

The below lines of code explains the usage of LINQ and also the advantages of using LINQ compared to a legacy approach. The code is shared below (the legacy code has been commented out). The method reads the XML data/file using LINQ. Below is the code snippet that I am sharing. As I said this has two versions: LINQ and Latest. 

The result will be stored in the Anonymous Class FileToWatch. Then this will be executed as a set of objects for looping through the items that we have read from the XML. 

C#
//Note: Below one is using the LINQ to XML
var path = Assembly.GetExecutingAssembly().Location;
path = path.Substring(0, path.LastIndexOf('\\')) + "\\" + "LocationsToWatch.xml";
XDocument xmlDoc = XDocument.Load(path);
files = (from x in xmlDoc.Root.Elements("location")
select
new FileToWatch
{
    Server = (string)x.Element("server").Value ?? string.Empty,
    Drive = (string)x.Element("drive").Value ?? string.Empty,
    Folder = (string)x.Element("folder").Value ?? string.Empty,
    FileName = (string)x.Element("filename").Value ?? string.Empty,
    SendMail = (string)x.Element("sendmail").Value ?? string.Empty,
    FullPath = x.Element("folder").Value + x.Element("filename").Value
}).ToList();

Legacy approach, using the DOM object.

C#
//Note: below code is legacy method of writing code to read XML in to Object
var xmlDoc = new XmlDocument();
xmlDoc.Load("LocationsToWatch.xml");
var itemNodes = xmlDoc.SelectNodes("locations");
foreach (XmlNode node in itemNodes)
{
    var location = node.SelectNodes("location");
    foreach (XmlNode loc in location)
    {
        var server = loc.SelectSingleNode("server").InnerText;
        var drive = loc.SelectSingleNode("drive").InnerText;
        var folder = loc.SelectSingleNode("folder").InnerText;
        var filename = loc.SelectSingleNode("filename").InnerText;
    } 
}

License

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


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)31-Oct-14 4:10
Shemeemsha (ഷെമീംഷ)31-Oct-14 4:10 
Nice tip KameshKannan Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup:
GeneralRe: My Vote 5 Pin
vaynenick11-Nov-16 18:16
vaynenick11-Nov-16 18:16 
GeneralSomething like this may work too using Navigator Pin
seee sharp17-Jan-13 20:16
seee sharp17-Jan-13 20:16 
GeneralRe: Something like this may work too using Navigator Pin
KameshKannan29-Jan-13 1:42
KameshKannan29-Jan-13 1:42 

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.