C# XML LINQ: A Basic Introduction

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.

  • Loading the XML content into a memory from files or streams.
  • Serializing the data to XML format and saving it to a file or stream.
  • Creating XML trees including nodes, elements, attribute, etc.
  • Querying an XML content (nodes) using LINQ queries.
  • Modification and deletion of an in-memory XML content (complete tree, nodes, elements, attributes, etc.).
  • Validation of XML content using XML Schema Definition (XSD).
  • Transforming XML documents from one format to the other using an EXtensible Style-sheet Language Transformation (XSLT).

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:

  • ‘XDeclaration’ class, which represents an XML declaration which identifies the document as an XML. It is always the first line of every XML document.
  • ‘XDocument’ class, which represents an XML document which contains important information regarding the validity of XML document. It can include ‘XDeclaration’, ‘XElement’ (root node), ‘XProcessingInstruction’, ‘XDocumentType’ and ‘XComment’.
  • ‘XDocumentType’ class, which represents an XML Document Type Definition (DTD) that defines the structure of an XML document.
  • ‘XNamespace’ class, which represents an XML namespace that is used to avoid any element naming conflicts in an XML document.
  • ‘XNode’ class, representing the abstract base class for ‘XComment’, ‘XContainer’, ‘XDocumentType’, ‘XProcessingInstruction’ and ‘XText’.
  • ‘XContainer’ class that represents an XML node that can contain other nodes.
  • ‘XElement’ class which represents an XML element.
  • ‘XAttribute’ class which represents an XML element attribute, a key value pair derived from ‘XObject’.
  • ‘XComment’ class that represents an XML comment.

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