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
MyReadXML();
//Console.WriteLine("for Appending in XML File");
//MyAppendXML(id, name, dept, day, month, year);
Console.ReadLine();
}
// 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.WriteStartDocument();
writer.WriteStartElement("Employees");
writer.WriteStartElement("Employee");
writer.WriteAttributeString("id", id);
writer.WriteElementString("name", name);
writer.WriteElementString("dept", dept);
writer.WriteStartElement("birthdate");
writer.WriteElementString("day", day);
writer.WriteElementString("month", month);
writer.WriteElementString("year", year);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
fs.Close();
}
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.
//reader.MoveToElement();
/*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.
reader.MoveToElement();
}
break;
case XmlNodeType.Text:
Console.WriteLine("\t" + reader.Value);
break;
}
}
reader.Close();
fs.Close();
}
public static void MyAppendXML(string id, string name, string dept, string day, string month, string year)
{
XmlDocument doc = new XmlDocument();
doc.Load("c:\\Employee.xml");
XmlNode root = doc.DocumentElement;
XmlElement emp = doc.CreateElement("Employee");
emp.SetAttribute("id", id);
emp.InnerXml = "
+ "
+ month + "
root.AppendChild(emp);
XmlWriter writer = new XmlTextWriter("c:\\Employee.xml", null);
doc.WriteTo(writer);
writer.Close();
}
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"?>
<Employees>
<Employee id="1">
<name>as</name>
<dept>as</dept>
<birthdate>
<day>1</day>
<month>1</month>
<year>1988</year>
</birthdate>
</Employee>
</Employees>
i have also attached the file that will show the output that we get on calling the read method after enter the employee details.