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.Let's have a Deep dive of this implementation.
Source Code :
Let me Brief What is XML ?
- 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
<?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 Magic of Thinking" Author="David Schwartz" Version="3.8.1" />
</BooksList>
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 Demonstration
Source Code :
Use the Following Namespaces
using System.Xml;
using System.IO;
using System.XML.XPath;
In Button Click Event Write the following Code :
using System.XML.XPath;
In Button Click Event Write the following Code :
try
{
string XmlPath = string.Empty;
XmlPath = Server.MapPath("~/XMLFiles/") + "BooksInfo.xml";
XmlDocument xdoc = new XmlDocument();
if (!File.Exists(XmlPath))
{
XmlDeclaration declaration = xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
XmlElement root = xdoc.CreateElement("BooksList");
XmlElement parent = xdoc.CreateElement("Book");
XmlAttribute BookName = xdoc.CreateAttribute("Name");
XmlAttribute Author = xdoc.CreateAttribute("Author");
XmlAttribute Version = xdoc.CreateAttribute("Version");
BookName.Value =
txtBookName.Text;
Author.Value = txtAuthorName.Text;
Version.Value =
txtVersion.Text;
xdoc.AppendChild(declaration);
xdoc.AppendChild(root);
root.AppendChild(parent);
parent.Attributes.Append(BookName);
parent.Attributes.Append(Author);
parent.Attributes.Append(Version);
xdoc.Save(XmlPath);
}
else
{
xdoc.Load(XmlPath);
XmlElement root = xdoc.DocumentElement;
XmlDocument exdoc = new XmlDocument();
exdoc.Load(XmlPath);
XmlNodeList nodeList = exdoc.SelectNodes("/BooksList/Book[@Name='" + txtBookName.Text + "']");
if (nodeList.Count > 0)
{
Response.Write("<script>alert('Book Name is already available
in the Directory...')</script>");
return;
}
XmlElement parent = xdoc.CreateElement("Book");
XmlAttribute BookName = xdoc.CreateAttribute("Name");
XmlAttribute Author = xdoc.CreateAttribute("Author");
XmlAttribute Version = xdoc.CreateAttribute("Version");
BookName.Value = txtBookName.Text;
Author.Value =
txtAuthorName.Text;
Version.Value =
txtVersion.Text;
root.AppendChild(parent);
parent.Attributes.Append(BookName);
parent.Attributes.Append(Author);
parent.Attributes.Append(Version);
xdoc.Save(XmlPath);
}
Response.Write("<script>alert('Data Inserted
Sucessfully...')</script>");
}
catch
{
Response.Write("<script>alert('Something went Wrong Try after
some time...')</script>");
}
No comments :
Post a Comment