CSharp lambda expressions in detail3


C# lambda
image credit:msdn.com

C# lambda expressions in detail part 3

In our C# lambda expressions in detail part 3 post, we will see the lambda expressions being used with XML(exrtensible markup language).


Extensible Markup Language (XML) is a markup language that rely on some particular rules for making the file or a document easier to understand by both humans and machines.
This markup has the beauty to be accessible without any concern about the platform or the database which the computer machine is using.

LINQ to XML is better and easier than XSLT Transformations but wait

As people are used to transforming XML from one format to another, most of them rely on XSLT transformation.This method is very traditional but what if we have another technique for getting the task done?
This is where comes the method of C#(serverside) or JavaScript(clientside) based XML operations.

A query language is very fruitful for extracting or manipulating the criteria based results . A transformation, would be useful to convert one XML representation of your data into another XML representation.

Now, lets see some working examples

XML file
<?xml version=”1.0″ encoding=”utf-8″ ?>
<Employees>
<Employee ID=”1″>
<Name>Robert</Name>
</Employee>
<Employee ID=”2″>
<Name>McGrath</Name>
</Employee>
<Employee ID=”3″>
<Name>Dennis</Name>
</Employee>
</Employees>

Code file
XDocument testXML = new XDocument();
XDocument.Load(“XMLFile1.xml”);

var employees = from employee in testXML.Descendants(“Employee”)
select new
{
ID = Convert.ToInt32(employee.Attribute(“ID”).Value),
Name = employee.Element(“Name”).Value
};

Lambda Expressions with Generic Collections

Generic Collections were introduced with C# 2.0. they provide a type safe mechanism to store and retrieve the data so the user can not input the data of mismatch types.
The common methods of traversing through these lists require a very large amount of programming code and doesn’t facilitate the art of programming aspect. In these sort of situations, the Lambda Expressions make the work very much easier to perform some logical operations when filtering our results.Here is my new post that clearly demonstrates how this can be achieved.

Lambda with Collections

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.