Saturday, 7 April 2018

How to Filter Data from XML File with XPath queries Using Asp.Net

Dear all In this article We will see How to Restrict Duplicate entries Insertion of Data into XML File Using Asp.Net.In my previous article i explained essence of XML,How to Insert the data into XML file Using Asp.Net,How to Restrict Duplicate entries Insertion of Data into XML File Using Asp.Net.Let's have a Deep dive of this implementation.




Let me Brief What is XPath ?
  • XPath represents XML Path Language
  • In order to Work with XPath Queries nee to incorporate System.XML.XPath namespace
  • XPath is a query Language which is used to query the xml document elements.
  • XPath Uses node related syntaxes in order to navigate to the particular node element
  • It Contains so many built in functions like Select Nodes(),Select() etc.....
  • By Using XPath Queries we can do so many data manipulations in an XML file.
  • XPath was recommended by World Wide Web Consortium


Let's See Structure of an Typical XML File

For Earlier XML File Structure Click Here

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<BooksList>
  <Book Name="Wings of Fire" Author="APJ Abdul Kalam" Version="1.3.1" />
  <Book Name="The Magic of Thinking" Author="David Schwartz" Version="3.8.1" />
  <Book Name="The Power of Habit" Author="Charles Duhigs" Version="5.1.8" />
  <Book Name="The Magic of Thinking" Author="David Schwartz" Version="" />
  <Book Name="The Magic of Thinking" Author="David Schwartz" Version="3.8.2" />

</BooksList>

Few XPath Queries :
For Multiple Attributes Filtration
Syntax :
exdoc.SelectNodes("/BooksList/Book[@Name='" + txtBookName.Text + "' and @Author='" + txtAuthorName.Text + "' and @Version='" + txtVersion.Text + "']");

For Selecting Enter XML Data
Syntax:
doc.SelectNodes("/EmployeeList/Employee");

Attributes Filtration with Contains
Syntax :

doc.SelectNodes("/EmployeeList/Employee[@Name[contains(.,'Vee')]]");


Steps to Work with XML
  • Go To Visual Studio Click on New-->Project-->Select Empty Website
  • Name the Application as Xml Demo and click on Ok
  • Go to Solution Explorer and Right Click on the Project -->Clik on Add New -->Folder Named it as Files
  • Now Design the Form With the Following Controls
  • Right Click on the Project -->Click on Add New Item -->Select Web Form Name it as InsertEmployee
  • Design the Form by Placing the following Code in InsertEmployee.aspx

Video Demonistration:

Source Code for Reading of Entire XML Data:

Namespaces Used in the Project :


using System.Xml;
using System.Xml.XPath;

using System.IO;

Button Click Event Code :

string path = string.Empty;
        path = Server.MapPath("~/XMLFiles/") + "BooksInfo.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(path);

        XmlNodeList xnlist = doc.SelectNodes("/BooksList/Book");

        for (int i = 0; i < xnlist.Count; i++)
        {
            string BookName = xnlist[i].Attributes["Name"].InnerText.ToString();
            string Author = xnlist[i].Attributes["Author"].InnerText.ToString();
            string version = xnlist[i].Attributes["Version"].InnerText.ToString();
            Response.Write(string.Format("Details are : {0}   {1}   {2}", BookName, Author, version) + "<br/>");
        }

Source Code for Contains of XPath Query:

Namespaces Used in the Project :


using System.Xml;
using System.Xml.XPath;
using System.IO;

Button Click Event Code :

string path = string.Empty;
        path = Server.MapPath("~/XMLFiles/") + "BooksInfo.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(path);

        XmlNodeList xnlist = doc.SelectNodes("/BooksList/Book[@Name[contains(.,'The')]]");

        for (int i = 0; i < xnlist.Count; i++)
        {
            string BookName = xnlist[i].Attributes["Name"].InnerText.ToString();
            string Author = xnlist[i].Attributes["Author"].InnerText.ToString();
            string version = xnlist[i].Attributes["Version"].InnerText.ToString();
            Response.Write(string.Format("Details are : {0}   {1}   {2}", BookName, Author, version) + "<br/>");
        }

No comments :

Post a Comment