Some examples in LINQ to XML
In this article, I will try to provide you with some examples like how to fetch xml, read specific attribute value, add new elements using LINQ, etc as a starting point so that you gain some knowledge on LINQ to XML.
LINQ to XML Operations
In this article, we will study about operations involved in LINQ to XML. Some examples on how to fetch xml, read specific attribute value, etc will help you get an idea about how stuffs work in LINQ to XML
Lets create a sample XML for Books and call it as Books.xml. The xml file is as below:XML File
< ?xml version="1.0" encoding="utf-8" ? >
< Books >
< Book >
< BookId >1< /BookId >
< AuthorName >Bill< /AuthorName >
< BookType >Education< /BookType >
< Price >$56< /Price >
< Publication >Wrox< /Publication >
Book >
< Book >
< BookId >2< /BookId >
< AuthorName >Scott< /AuthorName >
< BookType >Education< /BookType >
< Price >$26< /Price >
< Publication >Microsoft< /Publication >
Book >
< Book >
< BookId > 3 < /BookId >
< AuthorName >Tom< /AuthorName >
< BookType >marketing< /BookType >
< Price >$98< /Price >
< Publication >Ingress< /Publication >
Book >
< Book >
< BookId > 4 < /BookId >
< AuthorName >Dan< /AuthorName >
< BookType >Management< /BookType >
< Price >$26< /Price >
< Publication >Microsoft< /Publication >
Book >
< Book >
< BookId > 5 < /BookId >
< AuthorName >Tony< /AuthorName >
< BookType >Marketing< /BookType >
< Price >$33< /Price >
< Publication >Wrox< /Publication >
Book >
Now lets get started with LINQ details. Our aim is to fetch data from XML using LINQ.
The following namespaces are needed:
System.Collections.Generic;
System.Linq; System.Text;
System.Xml;
System.Xml.Linq;Read the XML
Lets read the entire XML using LINQ's XElement class.
XElement xEle = XElement.Load("Books.xml");
var allBooks = xEle.Elements();
foreach (var book in allBooks)
{
Console.WriteLine(books);
}
In the above example book is of type IEnumerableFetch all Author Names
Lets read the XMl and then filter out the author names.
XElement xEle = XElement.Load("Books.xml");
var allBooks = xEle.Elements();
foreach (var book in allBooks)
{
Console.WriteLine(books.Element("AuthorName").Value);
}Fetch a Specific Author by providing his Name
Lets code to fetch a specific xml element by providing author name
XElement xEle = XElement.Load("Books.xml");
var authorName = from author in xelement.Elements("Book")
where (string)author.Element("AuthorName") == "Bill"
select author;
foreach (var au in authorName)
{
Console.WriteLine(au.Element("AuthorName").Value);
}Apply sorting on elements by Price
Lets code to sort the books with price in ascending order.
XElement xEle = XElement.Load("Books.xml");
var price = from p in xelement.Elements("Book")
let pr = (string)code.Element("Price")
orderby pr
select pr;
foreach (var p in price)
{
Console.WriteLine(p);
}Get 2 Book items
Lets code to get the first 2 Book items from the XML file.
XElement xEle = XElement.Load("Books.xml");
var items = xEle.Descendents("Book").Take(2);
foreach (var i in items)
{
Console.WriteLine(i);
}Add new elements in Books.xml at Run time
Lets code to add new book element in Run time
XElement xEle = XElement.Load("Books.xml");
xEle.Add(new XElement("Book",
new XElement("AuthorName", "New Author"),
new XElement("Price", "$87")));
Console.Write(xEle);Delete an Element from Books.xml at Run time
Lets code to delete an element from the xml file using LINQ when a certain condition is met. For eg lets delete price element tag from the xml
XElement xEle = XElement.Load("Books.xml");
var author= xEle.Elements("Book").ToList();
foreach (XElement auth in author)
auth.SetElementValue("Author", null);
// or you can use
auth.RemoveAttributes();
Console.Write(xEle);Remove first 2 Elements using LINQ to XML
Lets code to remove first 2 element of your XML using LINQ
XElement xEle = XElement.Load("Books.xml");
var items = xEle.Descendents("Book").Take(2).Remove();
Console.WriteLine(items);
LINQ to XML gives you variety of options to fetch records based on your requirements.
Provided you with some basic things to get you started with LINQ to XML. You can use my examples and try your hands on and let me know if you encounter any problem somewhere.