Book Image

LINQ Quickly

By : N Satheesh Kumar
Book Image

LINQ Quickly

By: N Satheesh Kumar

Overview of this book

<p>This book gets you started with LINQ and shows how it will make your programming life easier by making use of new features from the .NET Framework 3.0. This book is split into seven chapters, each of which is dedicated to presenting a feature of LINQ and its usage in real-life scenarios. <br /><br />Language Integrated Query (LINQ) is a new feature in Visual Studio 2008 that extends its query capabilities, using C# and Visual Basic. Visual Studio 2008 comes with<br />LINQ provider assemblies that enable the use of LINQ with data sources such as in-memory collections, SQL relational databases, ADO.NET Datasets, XML documents, etc.<br />In Visual Studio 2008, Visual C# and Visual Basic are the languages that implement the LINQ language extensions. LINQ language extensions use the new standard query operators API, which is the query language for any collection that implements IEnumerable&lt;T&gt;.</p>
Table of Contents (14 chapters)
LINQ Quickly
Credits
About the Author
About the Reviewer
Preface
Building an ASP.NET Application
LINQ with Outlook

Loading and Traversing XML


Now that we've seen how to create a basic XML tree and the basic classes of LINQ to XML and its hierarchy, let's take a more detailed look at loading XML data and finding our way through it.

Loading XML

There are different ways of loading XML data. The XML data can be in the form of a file, or a string, or any other supported formats. Using LINQ to XML, we can load XML data through the XElement object. For example, to load XML data from a file, we can use the Load method of the XElement object, as given below:

// Loading XML
XElement LoadIcecreams = XElement.Load(@"c:\Icecreams.xml");

If the XML data is in the form of a string, we can load it through XElement by using the Parse method. For example, the code for loading Icecreams from string is as follows:

XElement LoadIcecreamsfromString = XElement.Parse(
@"<Icecream>
<Name>Rum Raisin Icecream</Name>
<Cholesterol>49mg</Cholesterol>
<VitaminA type=""VitaminA"">2g</VitaminA&gt...