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");

Output:

<?xmlversion="1.0"encoding="utf-16"?>
<!--This file contains student course data-->
<Students>
<Studentid="001">
<name>Mr. John</name>
<dept>Software</dept>
<dept_location>North</dept_location>
<course_name>C#</course_name>
<course_completion_Date>06/10/20XX</course_completion_Date>
</Student>
<Studentid="002">
<name>Miss. Doe</name>
<dept>Telecom</dept>
<dept_location>South</dept_location>
<course_name>Java</course_name>
<course_completion_Date>06/12/20XX</course_completion_Date>
</Student>
</Students>

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.

Conclusion

‘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

Top courses in C#

How to Program Voxel Worlds Like Minecraft with C# in Unity
Penny de Byl, Penny @Holistic3D.com
4.6 (1,238)
Learn C# for Beginners Crash Course
Tim Buchalka, Jean-Paul Roberts, Tim Buchalka's Learn Programming Academy
4.7 (538)
Design Patterns in C# Made Simple
Zoran Horvat
4.7 (49)
Highest Rated
Complete C# Unity Game Developer 2D
Ben Tristem, GameDev.tv Team, Rick Davidson
4.6 (77,167)
Bestseller
Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team
4.6 (21,241)
Bestseller

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 for Business.

Request a demo