Breaking News
Loading...
Monday, 10 December 2012

Add new node(Element) in existing xml file in C#.Net

first make form like :




-  now double click on button and write code like :

/////////////////////////   CODE    ////////////////////////////


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace Xml_File_Operation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         private void button3_Click(object sender, EventArgs e)
        {

           
               XmlDocument xdoc1 = new XmlDocument();
                xdoc1.Load("C:\\Users\\Nirav\\Desktop\\people1.xml");
                XmlNode person = xdoc1.CreateElement("person");
                XmlNode name = xdoc1.CreateElement("name");
                name.InnerText = textBox1.Text;
                person.AppendChild(name);

                XmlNode age = xdoc1.CreateElement("age");
                age.InnerText = numericUpDown1.Value.ToString();
                person.AppendChild(age);

                XmlNode email= xdoc1.CreateElement("email");
                email.InnerText = textBox3.Text;
                person.AppendChild(email);

                xdoc1.DocumentElement.AppendChild(person);
                xdoc1.Save("C:\\Users\\Nirav\\Desktop\\people1.xml");

            }

      }
}


//////////////////         OUTPUT      ////////////////////////



it make chanch like :


                  




--
/\/ir@\/  <(.'.)>

22 comments:

  1. Nice Block It is very helpful.... Thanks a lot for your help...

    ReplyDelete
  2. Your blog is loaded with informative post on .Net technology. Thanks for sharing. DOT NET Course in Chennai

    ReplyDelete
  3. very helpful thanks for sharing... :)

    ReplyDelete
  4. i created a page to add nodes and saving on sql server in one button.But the nodes and added lokk like this ...(nodes are added at straight)..why?
    plese sugggest any solutions?

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. This comment has been removed by a blog administrator.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. I have a problem. Visual Studio showed me this : "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Xml.dll

    Additional information: Cannot find file'C:\Users\Administrator\Desktop\test.xml'.

    ReplyDelete
    Replies
    1. Hello,
      First you have to make XML file and then save it to desktop with test.xml name and then run program

      Delete

Thanks for comment

 
Toggle Footer