Udemy logo

c# xml linqTo allow data communication between different systems having different deployment environments and implementations, a common data format is required. Extensible Markup Language (XML), which is a platform-independent, simple human readable format to represent simple as well as complex data. XML can be used for transferring data between different systems with different implementations and environments.

’LINQ’ (Language Integrated Query) is a set of features to extend a query capability of the C# language by providing an IntelliSence and syntax highlighting features. As ‘LINQ’ is integrated in a C# language therefore, debugging of the ‘LINQ’ queries are possible using the integrated debugger. LINQ can be used to query almost any collection of data including the XML DOM. This article explains how LINQ can be used to interact with XML documents.

Interested in learning more about C# LINQ? Take this course at Udemy.com

What is C# XML LINQ?

C# LINQ to XML can be considered a group of classes packaged in the ‘System.Xml.Linq’ namespace, which can be used to efficiently create, modify, delete and retrieve the data of an ‘XML’ file using ‘LINQ’ queries. The following are major functions provided by these classes.

C# XML LINQ Classes for XML Document

The ‘System.Xml.Linq’ namespace provides several classes to interact with XML content. These classes are used to perform different operations such as creation of an XML document, updating, insertion or deletion by modifying them. Some of the most  frequently used XML classes are:

The following code snippet illustrates the use of the above mentioned classes.

Code snippet:

XDocument doc = new XDocument (
new XDeclaration ("1.0", "utf-16", "true"),
new XComment ("This file contains the student course data"),
new XElement ("Students",
new XElement ("Student",
new XAttribute ("id", "001"),
new XElement ("name", "Mr. John"),
new XElement ("dept", "Software"),
new XElement ("dept_location", "North"),
new XElement ("course_name", "C#"),
new XElement ("course_completion_Date", "06/10/20XX")
new XElement ("Student",
new XAttribute ("id", "002"),
new XElement ("name", "Miss. Doe"),
new XElement ("dept", "Telecom"),
new XElement ("dept_location", "South"),
new XElement ("course_name", "Java"),
new XElement ("course_completion_Date", "06/12/20XX")
doc.Save ("D://StudentCourses.xml");


<!--This file contains student course data-->
<name>Mr. John</name>
<name>Miss. Doe</name>

New to C# LINQ? Check out this course at Udemy.com

C# XML LINQ Extension Class

The ‘System.Xml.Linq’ namespace ‘Extension’ class contains the extension methods that are used to retrieve the collections from an XML tree. Most of the methods in this class use ‘LINQ’ queries and operate on the collections. The following code snippets demonstrate the usage of some of these extension methods.

Code snippet 1:

using (StreamReader sr = newStreamReader ("D://StudentCourses.xml", true))
IEnumerable<XElement> students = (from student in
XDocument.Load (sr)
.Descendants ("Students")
select student).ToArray ();
foreach (XElement aStudent in students) {
Console.WriteLine ("Name: " + aStudent.Element ("name").Value);
Console.WriteLine ("Department: " + aStudent.Element ("dept").Value);
Console.WriteLine ("Course Name: " + aStudent.Element ("course_name").Value);
Console.WriteLine ("Course Completion Date: " + aStudent.Element ("course_completion_Date").Value);

Code snippet 2:

using (StreamReader sr = newStreamReader ("D://StudentCourses.xml", true))
XElement xelement = XElement.Load (sr);
IEnumerable<XElement> students = xelement.Elements ().ToArray ();

In the above code snippets, the ‘Extension’ class methods, ‘Descendants’ and ‘Elements’ are used, which are the axis methods that use ‘LINQ’ queries. A ‘Descendants’ method returns a collection of elements that are the descendant elements of a provided element name as an argument. An ‘Elements’ method returns a collection of the child elements of the specified parent element and document in the provided stream of an XML. The ‘Load’ method of an ‘XDocument’ and ‘XElement’ is used to load an XML stream into the memory. To retrieve the content of an element, an ‘Element’ method of the ‘XElement’ class is used which gets the first child element in the order of elements saved in a document with the specified ‘XName’ as an argument.

C# XML LINQ Queries for Retrieving Filtered Data

‘LINQ’ in C# language provides a clean, easy to maintain and understandable code. The following example demonstrates that how LINQ queries can be employed to retrieve the contents of an XML document.

Code snippet 1:

using (StreamReader sr = newStreamReader ("D://StudentCourses.xml", true))
XElement xelement = XElement.Load (sr);
IList < XElement> allStudent = (from singleStudent in xelement.Elements ()
select singleStudent).ToList ();

Code snippet 2:

using (StreamReader sr = newStreamReader ("D://StudentCourses.xml", true))
XElement xelement = XElement.Load (sr);
XElement aStudent = (from singleStudent in xelement.Elements ()
where singleStudent.Element ("name").Value.Contains ("John")
select singleStudent).FirstOrDefault ();

In the above C# code snippets, ‘LINQ’ queries are created to retrieve an XML content. In the first code snippet, all of the student records are retrieved from an XML file, but in the second code snippet only the record of a student with name ‘John’ as a part is extracted from a whole XML file.


‘C# XML LINQ’ is the set of classes used to perform several operations on XML data, which is a platform-independent human readable data and used as a communication medium between different systems. With ‘LINQ’ integration, it is possible to use XML content efficiently. Udemy.com contains several video courses on C# LINQ and XML.

To learn more about C# LINQ, take this course at Udemy.com

Page Last Updated: June 2014

Top courses in C#

Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (36,916)
C#/.NET - 50 Essential Interview Questions (Junior Level)
Krystyna Ślusarczyk
4.9 (272)
Highest Rated
C#/.NET - 50 Essential Interview Questions (Mid Level)
Krystyna Ślusarczyk
4.9 (103)
Highest Rated
C# Intermediate: Classes, Interfaces and OOP
Mosh Hamedani
4.5 (30,867)
Learn C# By Building Applications
Avetis Ghukasyan
4.6 (2,460)
Learn Parallel Programming with C# and .NET
Dmitri Nesteruk
4.5 (3,089)
Advanced Topics in C#
Dmitri Nesteruk
4.7 (294)

More C# Courses

C# students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo