---
title: "Adding multiple Records to the XML file"  
description: "Adding multiple Records to the XML file"  
author: "Anonymous User"  
published: 2013-09-04  
updated: 2013-09-04  
canonical: https://www.mindstick.com/forum/1442/adding-multiple-records-to-the-xml-file  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Adding multiple Records to the XML file

I would like to [add multiple](https://answers.mindstick.com/qa/115774/why-can-t-i-add-multiple-links-in-instagram-bio) [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) to the [xml file](https://www.mindstick.com/forum/360/how-to-read-xml-file-in-php) and here is the code which I’m using,

XmlTextWriter xwriter = new XmlTextWriter("C:\\Users\\[Desktop](https://answers.mindstick.com/qa/31257/what-is-desktop-os)\\TestFolder\\Xdoc1.xml", Encoding.UTF8);

xwriter.Formatting = Formatting.Indented;

xwriter.WriteStartElement("[Employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks)");

xwriter.WriteStartElement("Person");

xwriter.WriteStartElement("Name");

xwriter.WriteString(textBox1.Text);

xwriter.WriteEndElement();

xwriter.WriteStartElement("Designation");

xwriter.WriteString(textBox2.Text);

xwriter.WriteEndElement();

xwriter.WriteStartElement("Employee ID");

xwriter.WriteString(textBox3.Text);

xwriter.WriteEndElement();

xwriter.WriteStartElement("[Email](https://www.mindstick.com/articles/12870/10-easy-ways-to-manage-your-business-email-inbox)");

xwriter.WriteString(textBox4.Text);

xwriter.WriteEndElement();

xwriter.WriteEndElement();

xwriter.WriteEndElement();

xwriter.Close();

The [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) with this code is that only one record can be added. When i try to add the 2nd record, the [previous record](https://www.mindstick.com/forum/157521/store-the-previous-record-in-its-history-table-before-updating-it-in-the-sql-table-using-a-trigger) is overwritten.

## Replies

### Reply by Sumit Kesarwani

Hi Tanuj,\

Here is no need to convert xmlWriter class.

string xmlFile = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Candidates.xml");

xmldoc = new XmlDocument();

xmldoc.Load(xmlFile);

root = xmldoc.DocumentElement;

try

{

XmlNode CandidateNode = xmldoc.CreateNode(XmlNodeType.Element, "Candidate", "");

XmlNode id = xmldoc.CreateNode(XmlNodeType.Element, "CandidateId", "");

id.InnerText = "1";

CandidateNode.AppendChild(id);

XmlNode subPositionId = xmldoc.CreateNode(XmlNodeType.Element, "SubPositionId", "");

subPositionId.InnerText = candidate.PositionId.ToString();

CandidateNode.AppendChild(subPositionId);

XmlNode firstName = xmldoc.CreateNode(XmlNodeType.Element, "FirstName", "");

firstName.InnerText = candidate.FirstName;

XmlNode lastName = xmldoc.CreateNode(XmlNodeType.Element, "LastName", "");

lastName.InnerText = candidate.LastName;

CandidateNode.AppendChild(firstName);

CandidateNode.AppendChild(lastName);

root.AppendChild(CandidateNode);

xmldoc.Save(xmlFile);

This will help you.


---

Original Source: https://www.mindstick.com/forum/1442/adding-multiple-records-to-the-xml-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
