Tuesday, 31 December 2013

Enter Data in XML File Based on Customer Requirements



Example : Adding Element  in Customer.XML File Based on Customer Requirements


If customer want to add Name, Address and City then write below code.



Step 1 : First Create XML file named of customers.xml

Step 2 : Enter Data in customers.xml as follows.

<Customers>
  
  <Customer>
    <Name>Satyesh</Name>
    <Address>12/C, Yogjivan Society-1,Waghodia Road</Address>
    <City>Baroda</City>
  </Customer>

</Customers>

Step 3: Put 3 textbox and give named of 'txtname', 'txtadd' and 'txtcity' and put one button named of 'btnNewData' on default.aspx page and write code on button click event as follows.

private void btnNewData_Click(object sender, System.EventArgs e)
{
            String name = txtname.Text;
String add = txtadd.Text;
String city = txtcity.Text;

XmlDocument docXML = new XmlDocument();
            docXML.Load(server.mapPath("~/customers.xml"));
XmlElement elmXML = docXML.CreateElement("Customer");//Create Root Element
            string strNewPart =  "<Name>" + name + "</ Name >" +
                                            "<Address>" + add + "</Adderss>" +
                                             "<City>" + city + "</City>";

            elmXML.InnerXml = strNewPart;
            docXML.DocumentElement.AppendChild(elmXML); //Add New Child Element in customer.xml file
            docXML.Save(server.mapPath("~/"customers.xml"));
}


finally, user entered data stored in customers.xml file when you clicked on button.

enjoy................

Thanks To,

www.shreeinfotech.ind.in 

2 comments: