LINQ to XML provides an efficient and better option for working with XML documents.We work with in-memory XML using LINQ queries.LINQ to XML provides us the familiar Linq programming model in our programming language we are comfortable with such C#.This is more convenient than using the XPath queries in many scenarios.
We can query as well as modify the XML using LINQ to XML.
Using LINQ to XML
To start using LINQ to XML we use the Load() method of XElement class.Using this method populates the XElement instance with the XML document.
As XElement class is defined in the System.Xml.Linq namespace so we need to import this namespace.
For example the following will load the XElement with the contents of the XML file Students.xml.
XElement xelement = XElement.Load("..\\..\\Students.xml");
Students.xml file is defined as:
<?xml version="1.0" encoding="utf-8" ?> <Students> <Student> <RollNo>1</RollNo> <Name>Mark</Name> <Sex>Male</Sex> <Address> <Street>Street1</Street> <City>City1</City> <State>CA</State> <Zip>95220</Zip> <Country>USA</Country> </Address> </Student> <Student> <RollNo>2</RollNo> <Name>Robin</Name> <Sex>Female</Sex> <Address> <Street>Street1</Street> <City>City2</City> <State>CA</State> <Zip>95221</Zip> <Country>USA</Country> </Address> </Student> <Student> <RollNo>3</RollNo> <Name>Jenyy</Name> <Sex>Female</Sex> <Address> <Street>Street1</Street> <City>City2</City> <State>CA</State> <Zip>96121</Zip> <Country>USA</Country> </Address> </Student> </Students>
To access all the elements contained by the root Student element we use the following
IEnumerable<XElement> students = xelement.Elements();
The above will populate students with all the elements defined in the XML file.
We can iterate through all the elements using a foreach loop as:
foreach (var student in students) { Console.WriteLine(student); }
Fetching XML values using LINQ to XML
Accessing a specific element
We can also access a particular element in the XML by using the Element() method of the XElement object.To access the Address element we use the following
student.Element("Address")
Following will print all the Address elements
foreach (var student in students) { Console.WriteLine(student.Element("Address")); }
Selecting Elements having specific value
To filter elements having a specific value we use the Element() method of XElement instance and pass the element name.Finally we access the Value property to filter the elements.Following will return a student having RollNo 1.
var filteredRollNos = from student in students where student.Element("RollNo").Value == "1" select student; foreach (var item in filteredRollNos) { Console.WriteLine(item); }
Selecting attributes
If there are attributes defined in a element we can retrieve their values using the Attribute() method of the XElement class.
For example if we have student elements defined as:
<Student> <RollNo>1</RollNo> <Name>Mark</Name> <Sex>Male</Sex> <Marks subject="English">85</Marks> <Marks subject="Maths">70</Marks> <Address> <Street>Street1</Street> <City>City1</City> <State>CA</State> <Zip>95220</Zip> <Country>USA</Country> </Address> </Student>
then we can retrieve the name and marks of students as:
var studentMarks = from student in students where student.Element("Marks").Attribute("subject").Value == "English" select new { Name= student.Element("Name").Value, Marks= student.Element("Marks").Value };
By using the student.Element(“Marks”).Attribute(“subject”) ,we are able to fetch the Marks element having attribute value as “English”
Leave a Reply