Creating, reading and appending the XML file.

In this article we will read about the XML file. We will see how can we create an XML file, How can we Read an XML file and how can we append a XML file. We will do this in console application. we will see the reading of an XML file in 3 ways.

We are creating console application for this. for we ask the user to emter the employee details. And then call the read, write or delete method for the operations to perform.

static void Main(string[] args)
Console.WriteLine("Enter Employee Details:-");
Console.WriteLine("Enter Emp Id:-");
string id = Console.ReadLine();
Console.WriteLine("Enter Emp Name:-");
string name = Console.ReadLine();
Console.WriteLine("Enter Emp Dept:-");
string dept = Console.ReadLine();
Console.WriteLine("Enter Emp BirthDate:-");
Console.WriteLine("Enter Day:");
string day = Console.ReadLine();
Console.WriteLine("Enter Month:");
string month = Console.ReadLine();
Console.WriteLine("Enter Year:");
string year = Console.ReadLine();
// this method will create the xml file and ente rthe employee details in it.
MyWriteXML(id, name, dept, day, month, year);

Console.WriteLine("XmlFile is Generated");

Console.WriteLine("Xml File Is Reading");
// this method will read the xml file
//Console.WriteLine("for Appending in XML File");
//MyAppendXML(id, name, dept, day, month, year);


// this method will write the employee detail and if the file is not their then it will first create the file and then writ ein it.

public static void MyWriteXML(string id, string name, string dept, string day, string month, string year)
FileStream fs = new FileStream("c:\\Employee.xml", FileMode.OpenOrCreate, FileAccess.Write);
XmlTextWriter writer = new XmlTextWriter(fs, null);
writer.Formatting = Formatting.Indented;
writer.WriteAttributeString("id", id);
writer.WriteElementString("name", name);
writer.WriteElementString("dept", dept);
writer.WriteElementString("day", day);
writer.WriteElementString("month", month);
writer.WriteElementString("year", year);


this method will read the xml file node by node

public static void MyReadXML()
FileStream fs = new FileStream("c:\\Employee.xml", FileMode.Open, FileAccess.Read);
XmlTextReader reader = new XmlTextReader(fs);
reader.WhitespaceHandling = WhitespaceHandling.None;
while (reader.Read())
switch (reader.NodeType)
case XmlNodeType.Element:
Console.WriteLine(reader.Name + ":-");
if (reader.HasAttributes)
/*method 1*/
//Console.WriteLine("id:-\t" + reader.GetAttribute("id"));

/*method 2*/
//Console.WriteLine("Attributes of <" + reader.Name + ">");
//for (int i = 0; i < reader.AttributeCount; i++)
// Console.WriteLine(" {0}", reader[i]);
//// Move the reader back to the element node.

/*method 3*/
//Console.WriteLine("Attributes of <" + reader.Name + ">");
while (reader.MoveToNextAttribute())
Console.WriteLine(reader.Name + ":-\t" + reader.Value);
// Move the reader back to the element node.
case XmlNodeType.Text:
Console.WriteLine("\t" + reader.Value);


public static void MyAppendXML(string id, string name, string dept, string day, string month, string year)

XmlDocument doc = new XmlDocument();
XmlNode root = doc.DocumentElement;
XmlElement emp = doc.CreateElement("Employee");
emp.SetAttribute("id", id);
emp.InnerXml = "" + name + "" + dept
+ "
" + day + ""
+ month + "
" + year + "


XmlWriter writer = new XmlTextWriter("c:\\Employee.xml", null);


after entering the employee details when we call the MyWrtieXML() method we will get the following output. i.e. following xml file will be created at the given location.

<?xml version="1.0"?>
<Employee id="1">

i have also attached the file that will show the output that we get on calling the read method after enter the employee details.


  • Output we get when we call MyReadMethod. (44809-31053-Output-we-get-we-call-MyReadMethod.docx)
  • Comments

    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: