This is the second article of XML Manipulation using .NET and you can find the first article from here . In the first article I have discussed how to
1. Create an XML file
2. Modify an XML elements using XPath
3. Bind an XML document to Data Grid View
4. Delete an XML node using Xpath
In this article I'm going to demonstrate how to extract/ search data from an XML document using LINQ (Language INtegrated Query) and bind the extracted data to grid view. I'm going to use following XML file to search and grid view to bind the searched result.
Harry Potter J K. Rowling 2005 29.99 CHILDREN Everyday Italian Giada De Laurentiis 2005 30.00 COOKING XQuery Kick Start James McGovern 2010 49.99 WEB
I'm going to perform following actions
1. Search by author name
2. Search by year
3. Search by price
First of all I have to load the XML file into XDocument.
XDocument main = XDocument.Load(Server.MapPath(XMLManager.GetFilePath()));
Please note: XMLManager is a user defined class. You can enter the file path as a string or read article one for more details about XMLManager class.
Search by Author Name
private ListsearchByAuthor(XDocument doc ,string seachName) { var book = from c in doc.Descendants("book") where c.Element("author").Value.ToLower().Contains(seachName) //where c.Element("author").Value.StartsWith(seachName) //where c.Element("author").Value.EndsWith(seachName) select new Book { Id = Convert.ToInt32(c.Attribute("id").Value), Author = c.Element("author").Value, Year = Convert.ToInt32(c.Element("year").Value), Price = Convert.ToDouble(c.Element("price").Value), Category = c.Element("category").Value }; return book.ToList(); }
searchByAuthor method required two parameters, XDocument and the seachName. The above LINQ search through all the book elements and select the elements where author name contains the seachName and add them to book variable. Then return the list of books. You can use either StartsWith or EndsWith methods to search a string when necessary.
Search by Year
private ListsearchByYear(XDocument doc, int year) { var book = from c in doc.Descendants("book") where c.Element("year").Value == year.ToString() select new Book { Id = Convert.ToInt32(c.Attribute("id").Value), Author = c.Element("author").Value, Year = Convert.ToInt32(c.Element("year").Value), Price = Convert.ToDouble(c.Element("price").Value), Category = c.Element("category").Value }; return book.ToList(); }
searchByYear returns the list of books that match with the search criteria.
Search by Price
private ListsearchByPrice(XDocument doc, double minPrice, double maxPrice) { var book = from c in doc.Descendants("book") where Convert.ToDouble(c.Element("price").Value)> minPrice && Convert.ToDouble(c.Element("price").Value)<= maxPrice select new Book { Id = Convert.ToInt32(c.Attribute("id").Value), Author = c.Element("author").Value, Year = Convert.ToInt32(c.Element("year").Value), Price = Convert.ToDouble(c.Element("price").Value), Category = c.Element("category").Value }; return book.ToList(); }
searchByPrice method returns the list of books that the price comes between minimum and maximum value.
Bind to the data grid
Following code shows how to bind the book list to the grid view.
GridView1.DataSource = searchByPrice(main, 10, 30); GridView1.DataBind();