Sunday 29 April 2018

How to Update a record FROM XML File with XPATH Queries Using Asp.Net

Dear all In this article We will see How to Update 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,How to Delete a Record From XML 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" /> 
</BooksList>

For Loading of XML File


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

For Identifying of TagName

XmlNodeList nodes = doc.GetElementsByTagName(tagname);

For Updating of an XML Node

        foreach (XmlNode node in nodes)
        {
            bool IsEdit = false;
            foreach (XmlAttribute attribute in node.Attributes)
            {
                //Checking Whether Attribute Exit which we want to edit.
                if ((attribute.Name == txtReplacableAttributeName.Text) && (attribute.Value == txtReplacableAttributeValue.Text))
                {
                    IsEdit = true;
                }
                if (IsEdit == true)
                {
                    if (attribute.Name == txtReplacableAttributeName.Text)
                    {
                        attribute.Value = txtReplacingAttributeValue.Text;
                    }
                }
            }
        }

        doc.Save(XMLPath);

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 AspNetXMLDemo 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 UpdateXMLFile.aspx


Coding Part of this Example:

Design Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpdateData.aspx.cs" Inherits="XMLDemo.UpdateData" %>

<!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">
    <div>
   
    </div>
        <table class="auto-style1">
            <tr>
                <td>Enter Tag Name</td>
                <td>
                    <asp:TextBox ID="txtTagName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Replacable Attribute Name</td>
                <td>
                    <asp:TextBox ID="txtReplacableAttributeName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Replacable Attribute Value</td>
                <td>
                    <asp:TextBox ID="txtReplacableAttributeValue" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                 <td>Replacing Attribute Name</td>
                <td>
                    <asp:TextBox ID="txtReplacingAttributeName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
               <td>Replacing Attribute Value</td>
                <td>
                    <asp:TextBox ID="txtReplacingAttributeValue" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" />
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>

    </form>
</body>
</html>


No comments :

Post a Comment