Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a blank xml file i want to create a node in this using linq on button click
Posted

C#
protected void buttonAddClick(object sender, EventArgs e)
{
    try
    {
        XDocument xmlDoc = XDocument.Load(Server.MapPath("MyDoc.xml"));
        xmlDoc.Element("Employees").Add(
        new XElement("Employee", 
        new XElement("Name", txtName.Text),
        new XElement("City", txtCity.Text), 
        new XElement("Age", txtAge.Text)));
        
        xmlDoc.Save(Server.MapPath("MyEmployees.xml"));
    }
    catch{}
}
 
Share this answer
 
v3
Comments
Member 8214635 10-Oct-11 2:40am    
it work first time great but when i add a another node it replace first one.
refer this thread, might help you

http://forums.asp.net/t/1470779.aspx/1[^]
 
Share this answer
 
GanesanSenthilvel solution works fine with little change

C#
protected void buttonAddClick(object sender, EventArgs e)
{
    try
    {
        XDocument xmlDoc = XDocument.Load(Server.MapPath("MyDoc.xml"));
        xmlDoc.Element("Employees").Add(
        new XElement("Employee", 
        new XElement("Name", txtName.Text),
        new XElement("City", txtCity.Text), 
        new XElement("Age", txtAge.Text)));
        
        xmlDoc.Save(Server.MapPath("MyDoc.xml"));
    }
    catch{}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900