<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>
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; } } } }
<?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>