C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » XML »

Inserting data from textbox to XML.


Posted Date: 02 Sep 2009    Resource Type: Code Snippets    Category: XML
Author: satyaMember Level: Diamond    
Rating: 1 out of 5Points: 15



Inserting data from textbox to XML.



Description:


The below code is used to insert the data from textbox to XML file.

It checks whether the XML file already exists or not and if it is not

there it creates new file and inserts data. It also checks duplication

of data while inserting.And I am dispalying the data in Gridview.



Design:


<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
</table>


CodeBehind:


if (!Page.IsPostBack)
{
BindGrid();
}

public void BindDropDown()
{
if (!File.Exists("D:\\First2008Sample\\Master.xml"))
{
DataSet oDs = new DataSet();
oDs.ReadXml("D:\\First2008Sample\\Master.xml");
GridView1.DataSource = oDs;
GridView1.DataBind();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
if(Button1.Text == "Submit")
{
if (!File.Exists("D:\\First2008Sample\\Master.xml"))
{
XmlDocument oXmlDoc = new XmlDocument();
oXmlDoc.AppendChild(oXmlDoc.CreateXmlDeclaration("1.0", null, "yes"));


//......Creating Parent Locations Tag.

XmlElement oXmlPar = oXmlDoc.CreateElement("Locations");

//......Creating Child Nodes Under Parent Node.

XmlElement oXmlEleLoc = oXmlDoc.CreateElement("Location");

//......Creating Attributes In Child Node.

XmlAttribute oXmlAttrId = oXmlDoc.CreateAttribute("Id");
oXmlAttrId.Value = "1";

//......Appending Attributes To The Child Node.

oXmlEleLoc.Attributes.Append(oXmlAttrId);

//......Creating Elements To The Child Node.

XmlElement oXmlLocId = oXmlDoc.CreateElement("Location_Id");
XmlElement oXmlLocText = oXmlDoc.CreateElement("Location_Text");
XmlElement oXmlLocStatus = oXmlDoc.CreateElement("Status");

//......Assigning Values To Elements Under The Child Node.

oXmlLocId.AppendChild(oXmlDoc.CreateTextNode("Loc_1"));
oXmlLocText.AppendChild(oXmlDoc.CreateTextNode(TextBox1.Text));
oXmlLocStatus.AppendChild(oXmlDoc.CreateTextNode("U"));

//......Appending Elements To Child Nodes.

oXmlEleLoc.AppendChild(oXmlLocId);
oXmlEleLoc.AppendChild(oXmlLocText);
oXmlEleLoc.AppendChild(oXmlLocStatus);

//......Appending Child Nodes To Parent Node.

oXmlPar.AppendChild(oXmlEleLoc);
oXmlDoc.AppendChild(oXmlPar);

oXmlDoc.Save("D:\\First2008Sample\\Master.xml");

Response.Write("< script language='javascript' >alert('Location has been created successfully.'); location.href='CreateXML.aspx';< /script >");
}
else
{
XmlDocument oXmlDoc = new XmlDocument();
oXmlDoc.Load("D:\\First2008Sample\\Master.xml");

//......Verifying That Location Is Already Existing In The XML File.

XmlNode oXmlLoc = oXmlDoc.SelectSingleNode("Locations/Location[Location_Text='" + TextBox1.Text + "']");

//......Returns Null If It Doesnot Exists.

if (oXmlLoc == null)
{
int iNodeCnt = oXmlDoc.DocumentElement.ChildNodes.Count;
iNodeCnt++;

//......Creating Child Nodes Under Parent Node.

XmlElement oXmlEleLoc = oXmlDoc.CreateElement("Location");

//......Creating Attributes In Child Node.

XmlAttribute oXmlAttrId = oXmlDoc.CreateAttribute("Id");

oXmlAttrId.Value = iNodeCnt.ToString();
oXmlEleLoc.Attributes.Append(oXmlAttrId);
XmlElement oXmlLocId = oXmlDoc.CreateElement("Location_Id");
XmlElement oXmlLocText = oXmlDoc.CreateElement("Location_Text");
XmlElement oXmlLocStatus = oXmlDoc.CreateElement("Status");

oXmlLocId.AppendChild(oXmlDoc.CreateTextNode("Loc_" + iNodeCnt.ToString()));
oXmlLocText.AppendChild(oXmlDoc.CreateTextNode(TextBox1.Text));
oXmlLocStatus.AppendChild(oXmlDoc.CreateTextNode("U"));

oXmlEleLoc.AppendChild(oXmlLocId);
oXmlEleLoc.AppendChild(oXmlLocText);
oXmlEleLoc.AppendChild(oXmlLocStatus);

oXmlDoc.DocumentElement.AppendChild(oXmlEleLoc);

oXmlDoc.Save("D:\\First2008Sample\\Master.xml");
Response.Write("< script language='javascript' >alert('Location has been created successfully.'); location.href='CreateXML.aspx';< /script >");
}
else
{
Response.Write("< script language='javascript'>alert('Location already exists.');< /script >");
return;
}
}
}
}


Generated Output:

<?xml version="1.0" standalone="yes"?>
<Locations>
<Location Id="1">
<Location_Id>Loc_1</Location_Id>
<Location_Text>a</Location_Text>
<Status>U</Status>
</Location>
<Location Id="2">
<Location_Id>Loc_2</Location_Id>
<Location_Text>b</Location_Text>
<Status>U</Status>
</Location>
</Locations>



Responses

Author: Mohan    04 Sep 2009Member Level: Diamond   Points : 0
Good Article for newbies.
Keep on posting.



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Inserting data from textbox to XML.  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Write Xml using asp.net
Previous Resource: Creating XML file from database table using .Net
Return to Discussion Resource Index
Post New Resource
Category: XML


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use