top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to create XML file and insert data using ASP.NET?

0 votes
750 views
How to create XML file and insert data using ASP.NET?
posted Feb 19, 2016 by Sathaybama

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

0 votes

Description:

XML means Extensible markup language why we need to use this one because by using XML we can store the data and we can easily retrieve and display the data without using database in our applications.if we need to display dynamic data in our application it will take time to conenct database and retrive data from database but if use XML to store data we can do operations with xml file directly without using database. If we store data in database that is incompatible to some of the computer applications but if we store data in XML format it will support for all applications. It is independent for all software applications and it is accessible with all applications.

Now I will explain inserting data into xml and retrive data from XML and bind data to datalist with simple example.

Design your aspx page like this








Name: Location: Email: Comments:







Name: <%# DataBinder.Eval(Container.DataItem, "name") %>

E-mail: a rel="nofollow" href="mailto:%# DataBinder.Eval(Container.DataItem," target="_blank">"><%# DataBinder.Eval(Container.DataItem, "email") %>

Location: <%# DataBinder.Eval(Container.DataItem, "location") %>

Date: <%# DataBinder.Eval(Container.DataItem, "Date") %>

Description: <%# DataBinder.Eval(Container.DataItem, "Description") %>






After that add XML file to your application and give name as "Sample.xml" intially xml file like this root element is compulsary for XML files that’s why I added CommentInformation in XML file that root element in XML file.

<?xml version="1.0" encoding="utf-8"?>


After that add this namespace in codebehind

using System.Xml;
using System.Data;
After that write the following code in code behind

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Bind xml data to datalist
BindDatalist();
}
}
///
/// btnSubmit event is used to insert data into XML file
///
///


///
protected void btnSubmit_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("Sample.xml"));
XmlElement parentelement = xmldoc.CreateElement("Comments");
XmlElement name = xmldoc.CreateElement("Name");
name.InnerText = txtName.Text;
XmlElement location = xmldoc.CreateElement("location");
location.InnerText = txtLocation.Text;
XmlElement email = xmldoc.CreateElement("Email");
email.InnerText = txtEmail.Text;
XmlElement Description = xmldoc.CreateElement("Description");
Description.InnerText = txtComments.Text;
XmlElement date = xmldoc.CreateElement("Date");
date.InnerText = DateTime.Now.ToString();
parentelement.AppendChild(name);
parentelement.AppendChild(location);
parentelement.AppendChild(email);
parentelement.AppendChild(Description);
parentelement.AppendChild(date);
xmldoc.DocumentElement.AppendChild(parentelement);
xmldoc.Save(Server.MapPath("Sample.xml"));
BindDatalist();
}
///
/// Bind xml data to datalist
///
private void BindDatalist()
{
XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("Sample.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables.Count != 0)
{
dlComments.DataSource = ds;
dlComments.DataBind();
}
else
{
dlComments.DataSource = null;
dlComments.DataBind();
}
}
answer Feb 19, 2016 by Ajay Kumar Topno
0 votes

System.Xml namespace includes several classes to work with XML files. We can create a XML file or work with already created file. We can add parent and child nodes and attributes to these nodes using classes in System.Xml namespace. I will some of classes in this article to insert data into XML file.

1.Open MS Visual Studio 2010
2.File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
3.Select Web Location as File System and Click OK
4.From Menu, Website > Add New Item > Select Web Form and Click Add.
5.Drag and Drop a Button control and a Label Control or write code below in Default.aspx page

<h3>Click to create XML file and insert data</h3>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

6.Open code behind file and add following namespace in it

C#

using System.Xml;

VB.NET

Imports System.Xml

7.Write code below in Button click event.

C#

protected void Button1_Click(object sender, EventArgs e)
{
    string file = Server.MapPath("Customers.xml");

    XmlDocument doc = new XmlDocument();
    XmlNode decNode = doc.CreateXmlDeclaration("1.0", "utf-8", null);
    doc.AppendChild(decNode);

    XmlNode parentNode = doc.CreateElement("Customers");
    doc.AppendChild(parentNode);

    XmlNode childNode1 = doc.CreateElement("Customer");
    XmlAttribute id = doc.CreateAttribute("id");
    id.Value = "1";
    childNode1.Attributes.Append(id);
    parentNode.AppendChild(childNode1);

    XmlElement childElement1 = doc.CreateElement("Name");
    childElement1.InnerText = "James John";
    childNode1.AppendChild(childElement1);
    XmlElement childElement2 = doc.CreateElement("Phone");
    childElement2.InnerText = "(206) 555-9857";
    childNode1.AppendChild(childElement2);
    XmlElement childElement3 = doc.CreateElement("City");
    childElement3.InnerText = "Seattle";
    childNode1.AppendChild(childElement3);
    XmlElement childElement4 = doc.CreateElement("State");
    childElement4.InnerText = "WA";
    childNode1.AppendChild(childElement4);

    XmlNode childNode2 = doc.CreateElement("Customer");
    XmlAttribute id2 = doc.CreateAttribute("id");
    id2.Value = "2";
    childNode2.Attributes.Append(id2);
    parentNode.AppendChild(childNode2);

    XmlElement childElement5 = doc.CreateElement("Name");
    childElement5.InnerText = "Philip Wilson";
    childNode2.AppendChild(childElement5);
    XmlElement childElement6 = doc.CreateElement("Phone");
    childElement6.InnerText = "(206) 555-8122";
    childNode2.AppendChild(childElement6);
    XmlElement childElement7 = doc.CreateElement("City");
    childElement7.InnerText = "Redmond";
    childNode2.AppendChild(childElement7);
    XmlElement childElement8 = doc.CreateElement("State");
    childElement8.InnerText = "WA";
    childNode2.AppendChild(childElement8);

    doc.Save(file);
    Label1.Text = "XML File Created and data inserted successfully";
}

VB.NET

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim file As String = Server.MapPath("Customers.xml")

    Dim doc As New XmlDocument()
    Dim decNode As XmlNode = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
    doc.AppendChild(decNode)

    Dim parentNode As XmlNode = doc.CreateElement("Customers")
    doc.AppendChild(parentNode)

    Dim childNode1 As XmlNode = doc.CreateElement("Customer")
    Dim id As XmlAttribute = doc.CreateAttribute("id")
    id.Value = "1"
    childNode1.Attributes.Append(id)
    parentNode.AppendChild(childNode1)

    Dim childElement1 As XmlElement = doc.CreateElement("Name")
    childElement1.InnerText = "James John"
    childNode1.AppendChild(childElement1)
    Dim childElement2 As XmlElement = doc.CreateElement("Phone")
    childElement2.InnerText = "(206) 555-9857"
    childNode1.AppendChild(childElement2)
    Dim childElement3 As XmlElement = doc.CreateElement("City")
    childElement3.InnerText = "Seattle"
    childNode1.AppendChild(childElement3)
    Dim childElement4 As XmlElement = doc.CreateElement("State")
    childElement4.InnerText = "WA"
    childNode1.AppendChild(childElement4)

    Dim childNode2 As XmlNode = doc.CreateElement("Customer")
    Dim id2 As XmlAttribute = doc.CreateAttribute("id")
    id2.Value = "2"
    childNode2.Attributes.Append(id2)
    parentNode.AppendChild(childNode2)

    Dim childElement5 As XmlElement = doc.CreateElement("Name")
    childElement5.InnerText = "Philip Wilson"
    childNode2.AppendChild(childElement5)
    Dim childElement6 As XmlElement = doc.CreateElement("Phone")
    childElement6.InnerText = "(206) 555-8122"
    childNode2.AppendChild(childElement6)
    Dim childElement7 As XmlElement = doc.CreateElement("City")
    childElement7.InnerText = "Redmond"
    childNode2.AppendChild(childElement7)
    Dim childElement8 As XmlElement = doc.CreateElement("State")
    childElement8.InnerText = "WA"
    childNode2.AppendChild(childElement8)

    doc.Save(file)
    Label1.Text = "XML File Created and data inserted successfully"
End Sub

First we have established file path then we have created an object of XmlDocument class. We have created XML declaration node using XmlNode class. This declaration node is appended to our XML document using XmlDocument class AppendChild() method. Then I have created a parent node using same XmlNode class and appended this node to our XML file. Then a child node “Customer” is created for “Customers” parent node and added an attribute of “id” to it using XmlAttribute class. Then we have created elements in our child node and appended these elements to child node. We have also created appended another child node for parent node “Customers” and add elements to this child node as well. At the end we have to save our XML file.

8.Browse the website and click on button to see result

answer Feb 20, 2016 by Shivaranjini
0 votes

A friend suggested me to use ZetExcel.com Now I think it's one of the best
Try it.

answer Jul 15, 2020 by Jack Flint
Similar Questions
+2 votes

How do I properly align pdf...?? I have different tables for different run..

+3 votes

Hi i am developing College management system project..here i have to develop dynamic time table .....i have referred many websites but i didn't get any idea...i have mentioned my expected time table format below....it is high time to finish my project.can any one give me the correct reference or sent me the code for creating time table...very urgent..

enter image description here

...