LINQ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

LINQ XML

LINQ to XML

 

shape Introduction

This chapter demonstrates about the LINQ to XML which is used to access the all functionalities LINQ and also it is integrated with the .NET frame work. Following are the concepts are covered in this chapter.
  • LINQ to XML Introduction
  • Exapmles of LINQ to XML

LINQ to XML Introduction

shape Description

By Using the LINQ to XML user can load the new XML Document into the memory and easily makes the modification of the document. User can save the XML document in a memory and also sort the order of the document for these operations user need not to have the more knowledge about the XML. Some classes are listed which are useful to work with the LINQ to XML Document which are listed below.
  • XAttribute
  • Xcomment
  • XCData
  • XDocument
  • XContainer
  • XElement
  • XDecleration
  • XNamespace
  • XName
  • XNode
  • XNodeEqualityComparer
  • XNodeDocumentOrderComparer
  • XObjectChange
  • XObject
  • XObjectEventHandler
  • XObjectChangeEventArgs
  • XText
  • XProcessingInstruction

Examples of LINQ to XML

shape Description

The LINQ to XML  examples demonstrates the inserting the documents into the memory and saving the document in memory and deleting the document those examples are shown below.The code below demonstrates the reading of the XML document using LINQ is as shown. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; namespace LINQtoXML { class ExampleOfXML { static void Main(string[] args) { string myXML = @"<Departments> <Department>Finance</Department> <Department>Sales</Department> <Department>Max-Sales</Department> <Department>Marketing</Department> </Departments>"; XDocument xdoc = new XDocument(); xdoc = XDocument.Parse(myXML); var result = xdoc.Element("Departments").Descendants(); foreach (XElement item in result) { Console.WriteLine("Department Name - " + item.Value); } Console.WriteLine("\nPress any key to continue."); Console.ReadKey(); } } } [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below. The code below demonstrates the reading of the XML document using LINQ in VB [csharp] Imports System.Collections.Generic Imports System.Linq Imports System.Xml.Linq Module Module1 Sub Main(ByVal args As String()) Dim myXML As String = "<Departments>" & vbCr & vbLf & "<Department>Finance</Department>" & vbCr & vbLf & "<Department>Sales</Department>" & vbCr & vbLf & "<Department>Max-Sales</Department>" & vbCr & vbLf & "<Department>Marketing</Department>" & vbCr & vbLf & "</Departments>" Dim xdoc As New XDocument() xdoc = XDocument.Parse(myXML) Dim result = xdoc.Element("Departments").Descendants() For Each item As XElement In result Console.WriteLine("Department Name - " + item.Value) Next Console.WriteLine(vbLf & "Press any key to continue.") Console.ReadKey() End Sub End Module [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below. The code below demonstrates the adding new note to the XML document in C# is as shown. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; namespace LINQtoXML { class ExampleOfXML { static void Main(string[] args) { string myXML = @"<Departments> <Department>Finance</Department> <Department>Sales</Department> <Department>Max-Sales</Department> <Department>Marketing</Department> </Departments>"; XDocument xdoc = new XDocument(); xdoc = XDocument.Parse(myXML); //Add new Element xdoc.Element("Departments").Add(new XElement("Department", "Finance")); //Add new Element at First xdoc.Element("Departments").AddFirst(new XElement("Department", "Support")); var result = xdoc.Element("Departments").Descendants(); foreach (XElement item in result) { Console.WriteLine("Department Name - " + item.Value); } Console.WriteLine("\nPress any key to continue."); Console.ReadKey(); } } } [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below. The code below demonstrates the adding new note to the XML document in VB is as shown. [csharp] Imports System.Collections.Generic Imports System.Linq Imports System.Xml.Linq Module Module1 Sub Main(ByVal args As String()) Dim myXML As String = "<Departments>" & vbCr & vbLf & "<Department>Finance</Department>" & vbCr & vbLf & "<Department>Sales</Department>" & vbCr & vbLf & "<Department>Max-Sales</Department>" & vbCr & vbLf & "<Department>Marketing</Department>" & vbCr & vbLf & "</Departments>" Dim xdoc As New XDocument() xdoc = XDocument.Parse(myXML) xdoc.Element("Departments").Add(New XElement("Department", "Finance")) xdoc.Element("Departments").AddFirst(New XElement("Department", "Support")) Dim result = xdoc.Element("Departments").Descendants() For Each item As XElement In result Console.WriteLine("Department Name - " + item.Value) Next Console.WriteLine(vbLf & "Press any key to continue.") Console.ReadKey() End Sub End Module [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below. The code below demonstrates the Deleting a particular node from the XML Document which is shown in C# is as shown below. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; namespace LINQtoXML { class ExampleOfXML { static void Main(string[] args) { string myXML = @"<Departments> <Department>Support</Department> <Department>Finance</Department> <Department>Sales</Department> <Department>Max-Sales</Department> <Department>Marketing</Department> <Department>Accounts</Department> </Departments>"; XDocument xdoc = new XDocument(); xdoc = XDocument.Parse(myXML); //Remove Sales Department xdoc.Descendants().Where(s =>s.Value == "Sales").Remove(); var result = xdoc.Element("Departments").Descendants(); foreach (XElement item in result) { Console.WriteLine("Department Name - " + item.Value); } Console.WriteLine("\nPress any key to continue."); Console.ReadKey(); } } } [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below. The code below demonstrates the Deleting a particular node from the XML Document which is shown in VB is as shown below. [csharp] Imports System.Collections.Generic Imports System.Linq Imports System.Xml.Linq Module Module1 Sub Main(args As String()) Dim myXML As String = "<Departments>" & vbCr & vbLf & "<Department>Support</Department>" & vbCr & vbLf & "<Department>Finance</Department>" & vbCr & vbLf & "<Department>Sales</Department>" & vbCr & vbLf & "<Department>Max-Sales</Department>" & vbCr & vbLf & "<Department>Marketing</Department>" & vbCr & vbLf & "<Department>Accounts</Department>" & vbCr & vbLf & "</Departments>" Dim xdoc As New XDocument() xdoc = XDocument.Parse(myXML) xdoc.Descendants().Where(Function(s) s.Value = "Sales").Remove() Dim result = xdoc.Element("Departments").Descendants() For Each item As XElement In result Console.WriteLine("Department Name - " + item.Value) Next Console.WriteLine(vbLf & "Press any key to continue.") Console.ReadKey() End Sub End Module [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below.

Summary

shape Points

  • LINQ offers easy accessibility to all the documents.
  • LINQ to XML is integrated with the .NET Frame Work.