Friday 13 April 2018

How to Delete a Record From XML File With XPATH Queries Using Asp.Net

Dear all In this article We will see How to Delete a Record From XML File with XPATH queries 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,
How to Filter Data from XML File with XPath queries 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

<?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="3.8.2" />
  <Book Name="The Magic of Thinking" Author="David Schwartz" Version="3.8.3" />
</BooksList>

For Loading of XML File

XmlDocument doc = new XmlDocument();
doc.Load(path);

For Identifying of TagName

XmlNodeList nodes = doc.GetElementsByTagName(tagname);


For Deleting of an XML Node


foreach (XmlNode node in nodes)
            {
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    if ((attribute.Name == searchconditionAttributename) && (attribute.Value == searchconditionAttributevalue))                    
                    {                        
                        node.RemoveAll();
                        break;
                    }
                }
            }

For Saving of an XML File

doc.Save(path);

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 DeleteNode.aspx



Coding Part of this Example:


Design Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DeleteNode.aspx.cs" Inherits="DeleteNode" %>

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table class="auto-style1">
            <tr>
                <td>Enter Tag Name</td>
                <td>
                    <asp:TextBox ID="txtTagName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter Attribute Name</td>
                <td>
                    <asp:TextBox ID="txtAttributeName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter Attribute Value</td>
                <td>
                    <asp:TextBox ID="txtAttributeValue" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" />
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
    <div>
   
    </div>
    </form>
</body>
</html>



Namespaces Used in the Project :

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

DeleteXMLNode Method Code:

  public void DeleteXMLNode(string path, string tagName, string attributeName, string attributeValue)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        XmlNodeList nodes = doc.GetElementsByTagName(tagName);
        foreach (XmlNode node in nodes)
        {
            foreach (XmlAttribute attribute in node.Attributes)
            {
                if (attribute.Name.Equals(attributeName, StringComparison.InvariantCultureIgnoreCase) && attribute.Value.Equals(attributeValue, StringComparison.InvariantCultureIgnoreCase))
                {
                    node.RemoveAll();
                    break;
                }
            }
        }
        doc.Save(path);
    }


Button Click Event Code :


protected void btnDelete_Click(object sender, EventArgs e)

    {
        string Path = string.Empty;
        Path = Server.MapPath("~/XMLFiles/") + "BooksInfo.xml";
        DeleteXMLNode(Path, txtTagName.Text, txtAttributeName.Text, txtAttributeValue.Text);
        Response.Write("<script>alert('Data Deleted Sucessfully')</script>");
    }


No comments :

Post a Comment