Thursday, 29 March 2012

How to Add new Elements to Existing Xml Documents?


    How to add new Element to existing xml document?

           XmlDocument xmldoc = new XmlDocument();
           xmldoc.Load("C:\\jobs.xml");   // Loading xml file to memory
          
           XmlElement newjob = xmldoc.CreateElement("jobs"); // parent
           XmlElement jobid = xmldoc.CreateElement("job_ids");
           jobid.InnerText = txtJobid.Text;
           XmlElement jobname = xmldoc.CreateElement("job_desc");
           jobname.InnerText = txtJobdesc.Text;
           XmlElement minsal = xmldoc.CreateElement("min_lvl");
           minsal.InnerText = txtminlvl.Text;
           XmlElement maxsal = xmldoc.CreateElement("max_lvl");
           maxsal.InnerText = txtmaxlvl.Text;
           newjob.AppendChild(jobid);
           newjob.AppendChild(jobname);
           newjob.AppendChild(minsal);
           newjob.AppendChild(maxsal);
           xmldoc.DocumentElement.AppendChild(newjob);
           Label1.Text = "dadasdlfkjaskld";
           xmldoc.Save("C:\\jobs.xml");     // the the xml file to disk
            

No comments:

Post a Comment