adding form data to xml file using asp.net

XML stands for Extensible Markup Language

XML is a markup language is just like html

HTML is to display the data and XML is to represent the data.

original article from

http://www.codeforasp.net/save-form-data-to-xml-file-at-runtime.html

XML was designed to carry and store data.

HTML was designed to display the data

System.Xml Namespace

The System.Xml namespace provides standards-based support for processing XML. The supported standards are:

* XML 1.0 – http://www.w3.org/TR/1998/REC-xml-19980210 – including DTD support.

* XML Namespaces – http://www.w3.org/TR/REC-xml-names/ – both stream level and DOM.

* XSD Schemas – http://www.w3.org/2001/XMLSchema

* XPath expressions – http://www.w3.org/TR/xpath

* XSLT transformations – http://www.w3.org/TR/xslt

* DOM Level 1 Core – http://www.w3.org/TR/REC-DOM-Level-1/

* DOM Level 2 Core – http://www.w3.org/TR/DOM-Level-2/

XMLDocument: The DOM is an in-memory (cache) tree representation of

an XML document and enables the navigation and editing of this

document.

XMLNode: XmlNode is the base class in the.NET implementation of the

DOM. It supports XPath selections and provides editing capabilities.

The XmlDocument class extends XmlNode and represents an XML
document. You can use XmlDocument to load and save XML data. It also
includes methods for node creation.

Start your Application
step1: Open visual studio –> File –> New –> Website –>ok

step2: Design the form as below in Default.aspx page

Then goto control properties change id’s of controls

Ordercode textbox ID is txtOrderCode
DateOfOrder TextBox ID is txtDateOfOrder
Location TextBox ID is txtLocation
Content TextBox ID is txtContent
TotalItems TextBox ID is txtTotalCount
Button ID is btnSave

xmlorder

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table style=”border: 1px solid #000000; font-family: Arial, Helvetica, sans-serif; font-size: 9pt;”>
<tr>
<td colspan=”2″ style=”background-color: #DBDBDB;” >
<b>Order Report Details:</b>
</td>
</tr>
<tr>
<td >
OrderCode:
</td>
<td >
<asp:TextBox ID=”txtOrderCode” runat=”server” Width=”201px”></asp:TextBox>
</td>
</tr>
<tr>
<td >
Date Of Order:
</td>
<td >
<asp:TextBox ID=”txtDateOfOrder” runat=”server” Width=”201px”></asp:TextBox>
</td>
</tr>
<tr>
<td >
Location:
</td>
<td >
<asp:TextBox ID=”txtLocation” runat=”server” Width=”201px”></asp:TextBox>
</td>
</tr>
<tr>
<td style=”width: 101px; height: 41px”>
Content:
</td>
<td style=”width: 204px; height: 41px”>
<asp:TextBox ID=”txtContent” runat=”server” Width=”201px”></asp:TextBox>
</td>
</tr>
<tr>
<td >
Total Items:
</td>
<td >
<asp:TextBox ID=”txtTotalCount” runat=”server” Width=”201px”></asp:TextBox>
</td>
</tr>
<tr>
<td colspan=”2″ style=”width: 101px; height: 41px”>
<asp:Button Text=”Save” runat=”server” ID=”btnSave” Width=”95px” OnClick=”btnSave_Click”/>
</td>
</tr>
<tr>
<td colspan=”2″ style=”width: 101px; height: 41px”>
<asp:Label Text=”message” runat=”server” ID=”lblResult” Width=”295px”/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

step3: In btnSave button click, the code explains how to create XML file at runtime and checks already XML file exists or not using System.IO namepsace

code looks like

using System.Xml  //add this namepsace

string xmlPath = MapPath(“Order.xml”);

XmlDocument doc = new XmlDocument();

//Check if the file already exists or not

if (System.IO.File.Exists(xmlPath))
{
doc.Load(xmlPath);
XmlNode orderNode = CreateOrderNode(doc);
//Get reference to the order node and append the order node to it
XmlNode orderStoreNode = doc.SelectSingleNode(“orderReport”);
orderStoreNode.AppendChild(orderNode);
lblResult.Text = “XML Document has been successfully updated”;
}

else
{
XmlNode declarationNode = doc.CreateXmlDeclaration(“1.0″, “”, “”);
doc.AppendChild(declarationNode);

XmlNode orderStoreNode = doc.CreateElement(“orderReport”);  //Order Report main Directory node
XmlNode orderNode = CreateOrderNode(doc); //this will create SubDirectory nodes and its child nodes

//Append the Order node to the Orderstore node
orderStoreNode.AppendChild(orderNode);
//Append the orderstore node to the document
doc.AppendChild(orderStoreNode);
lblResult.Text = “XML Document has been successfully created”;
}
doc.Save(xmlPath);  //complete document will save here

step4: Write code for creating SubDirectory Node and its children nodes using XmlNode function CreateOrderNode(doc)

sample code looks like

public XmlNode CreateOrderNode(XmlDocument doc)
{
XmlNode orderNode = doc.CreateElement(“report”);

//Add all the children of the Order node  i.e sub directory node

XmlNode orderCodeNode = doc.CreateElement(“orderCode”);
orderCodeNode.InnerText = txtOrderCode.Text;
orderNode.AppendChild(orderCodeNode);

XmlNode dateNode = doc.CreateElement(“Date”);
dateNode.InnerText = txtDateOfOrder.Text;
orderNode.AppendChild(dateNode);

XmlNode location = doc.CreateElement(“Location”);
location.InnerText = txtLocation.Text;
orderNode.AppendChild(location);

XmlNode content = doc.CreateElement(“Content”);
content.InnerText = txtContent.Text;
orderNode.AppendChild(content);

XmlNode countNode = doc.CreateElement(“Count”);
countNode.InnerText = txtTotalCount.Text;
orderNode.AppendChild(countNode);

return orderNode;
}

Complete code looks like in Default.aspx.cs

using System.Xml;
———————————————————————————–
protected void btnSave_Click(object sender, EventArgs e)
{
string xmlPath = MapPath(“Order.xml”);
XmlDocument doc = new XmlDocument();
//Check if the file already exists or not
if (System.IO.File.Exists(xmlPath))
{
doc.Load(xmlPath);
XmlNode orderNode = CreateOrderNode(doc);
//Get reference to the order node and append the order node to it
XmlNode orderStoreNode = doc.SelectSingleNode(“orderReport”);
orderStoreNode.AppendChild(orderNode);
lblResult.Text = “XML Document has been successfully updated”;
}
else
{
XmlNode declarationNode = doc.CreateXmlDeclaration(“1.0″, “”, “”);
doc.AppendChild(declarationNode);

XmlNode orderStoreNode = doc.CreateElement(“orderReport”);  //main Directory node
XmlNode orderNode = CreateOrderNode(doc); //sub directory node

//Append the Order node to the Orderstore node
orderStoreNode.AppendChild(orderNode);
//Append the orderstore node to the document
doc.AppendChild(orderStoreNode);
lblResult.Text = “XML Document has been successfully created”;
}
doc.Save(xmlPath);
}

——————————————————————————

public XmlNode CreateOrderNode(XmlDocument doc)
{
XmlNode orderNode = doc.CreateElement(“report”);

//Add all the children of the Order node  i.e sub directory node

XmlNode orderCodeNode = doc.CreateElement(“orderCode”);
orderCodeNode.InnerText = txtOrderCode.Text;
orderNode.AppendChild(orderCodeNode);

XmlNode dateNode = doc.CreateElement(“Date”);
dateNode.InnerText = txtDateOfOrder.Text;
orderNode.AppendChild(dateNode);

XmlNode location = doc.CreateElement(“Location”);
location.InnerText = txtLocation.Text;
orderNode.AppendChild(location);

XmlNode content = doc.CreateElement(“Content”);
content.InnerText = txtContent.Text;
orderNode.AppendChild(content);

XmlNode countNode = doc.CreateElement(“Count”);
countNode.InnerText = txtTotalCount.Text;
orderNode.AppendChild(countNode);

return orderNode;
}

Now Run the Application see the output. First time when you run the application it will create the file order.xml by default i defined in the project

Enter the values of OrderDetails form then stop the project and refresh the complete project (right click on project click in refresh button)  then order.xml will generate then open order.xaml file you can see the order details what you have entered

Happy coding

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.