How to read XML document using C# LINQ
Read XML document using LINQ C#
In this article we will see how to read XML document using
C#. At first we have to create sample XML document with some sample data. In below,
we have created one sample XML document with two employee elements.
<Start>
<employees>
<employee>
<id>1</id>
<name>sourav kayal</name>
</employee>
<employee>
<id>2</id>
<name>dotnetFunda.com</name>
</employee>
</employees>
</Start>
Read all value from XML Document
At first we will see how to read
value of all elements. In example, you can find simple LINQ query to fetch all
elements from XML document. Here we have used ElementAt() function to get
values from certain XML Element.
using System;
using System.Collections;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
namespace BridgeDesign
{
class Program
{
static void Main(string[] args)
{
XDocument document = XDocument.Load("D:\\test.xml");
var selectedEmp = from r in document.Descendants("employee")
select r;
XElement a = selectedEmp.ElementAt(0);
Console.WriteLine("Employee ID:-" + a.Element("id").Value + " Employee Name:- "+ a.Element("name").Value);
XElement b = selectedEmp.ElementAt(1); Console.WriteLine("Employee
ID:-" + b.Element("id").Value
+ "
Employee Name:- " + b.Element("name").Value);
Console.ReadLine();
}
}
}
Our sample
XML document has only two employs. Here is sample output.
Read node value with condition
In this example we will see how to get particular node
depending on certain condition. For example we would like to see information
only that employee whose ID = 1. We can modify LINQ query of previous example.
using System;
using System.Collections;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
namespace BridgeDesign
{
class Program
{
static void Main(string[] args)
{
XDocument document = XDocument.Load("D:\\test.xml");
var selectedEmp = from r in document.Descendants("employee").Where (r => (string)r.Element("id").Value
== "1")
select r;
XElement a = selectedEmp.ElementAt(0);
Console.WriteLine("Employee ID:-" + a.Element("id").Value + "
Employee Name:- "+ a.Element("name").Value);
Console.ReadLine();
}
}
}
Output is here.
Search Element by Attribute Value
Now we will search XML element by its attribute value. For that,
at first we will modify our old XML document like below.
<Start>
<employees>
<employee type="HR">
<id>1</id>
<name>sourav kayal</name>
</employee>
<employee type="Developer">
<id>2</id>
<name>dotnetFunda.com</name>
</employee>
</employees>
</Start>
Here is C# sample program to search element by attribute
value.
using System;
using System.Collections;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
namespace BridgeDesign
{
class Program
{
static void Main(string[] args)
{
XDocument document = XDocument.Load("D:\\test.xml");
var selectedBook = from r in document.Descendants("employee").Where(r
=> (string) r.Attribute("type").Value=="HR")
select r;
XElement a = selectedBook.ElementAt(0);
Console.WriteLine("Employee ID:-" + a.Element("id").Value + " Employee Name:- "+ a.Element("name").Value);
Console.ReadLine();
}
}
}
Here is sample output.
Conclusion:
In this article we looked into how to read XML document using C# and
LINQ query. Hope you have understood the basic concept of reading XML document.