LINQ XML - LINQ

What is LINQ to XML?

LINQ to XML provides easy accessibility to all LINQ functionalities like standard query operators, programming interface, etc. It is integrated with .NET framework as well as LINQ to XML also makes the best use of .NET framework functionalities like debugging, compile-time checking, strong typing and many more to say.

What is the introduction of LINQ to XML?

When you are using LINQ to XML, it is very easy to load XML documents into memory as well as querying and document modification. You can save XML documents in existing memory to disk and serialize them. LINQ to XML ignores the need for a developer to learn the XML query language which is not simple to learn.
LINQ to XML has its power in the System.Xml.Linq namespace. This has all the 19 necessary classes to work with XML. These classes are the following ones.
  • XAttribute
  • XCData
  • XComment
  • XContainer
  • XDeclaration
  • XDocument
  • XDocumentType
  • XElement
  • XName
  • XNamespace
  • XNode
  • XNodeDocumentOrderComparer
  • XNodeEqualityComparer
  • XObject
  • XObjectChange
  • XObjectChangeEventArgs
  • XObjectEventHandler
  • XProcessingInstruction
  • XText

Read the XML File using LINQ

C#

##START33
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>Account</Department>
<Department>Sales</Department>
<Department>Pre-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();
}
}
}

VB

Once you compile and execute the above code of C# or VB then it produces the following result:

Add New Node

C#

VB

Once you compile and execute the above code of C# or VB then it produces the following result:

Deleting Particular Node

C#

VB

Once you compile and execute the above code of C# or VB then it produces the following result:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

LINQ Topics