Write to XML File......
private static void Write2XMLFile()
{
XmlTextWriter myXmlTextWriter = new XmlTextWriter (@"D:\Study\newEmployees.xml", null);
myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument(false);
//myXmlTextWriter.WriteDocType("Employees", null, "Employees.dtd", null);
myXmlTextWriter.WriteComment("This file represents another fragment of a organization's employee's database");
myXmlTextWriter.WriteStartElement("Employees");
myXmlTextWriter.WriteStartElement("Employee", null);
myXmlTextWriter.WriteAttributeString("Name","Shriniwas Wani");
myXmlTextWriter.WriteAttributeString("BirthDate","14 August 1980");
myXmlTextWriter.WriteAttributeString("Location","Pune");
myXmlTextWriter.WriteElementString("ContactNumber", null, "9890098900");
myXmlTextWriter.WriteStartElement("Department", null);
myXmlTextWriter.WriteElementString("Engineering", "SE");
myXmlTextWriter.WriteElementString("Management", "M2");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteElementString("Salary", "2000");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();
//Write the XML to file and close the myXmlTextWriter
myXmlTextWriter.Flush();
myXmlTextWriter.Close();
Console.WriteLine("Written to File");
}
It produces output as follows :
{
XmlTextWriter myXmlTextWriter = new XmlTextWriter (@"D:\Study\newEmployees.xml", null);
myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument(false);
//myXmlTextWriter.WriteDocType("Employees", null, "Employees.dtd", null);
myXmlTextWriter.WriteComment("This file represents another fragment of a organization's employee's database");
myXmlTextWriter.WriteStartElement("Employees");
myXmlTextWriter.WriteStartElement("Employee", null);
myXmlTextWriter.WriteAttributeString("Name","Shriniwas Wani");
myXmlTextWriter.WriteAttributeString("BirthDate","14 August 1980");
myXmlTextWriter.WriteAttributeString("Location","Pune");
myXmlTextWriter.WriteElementString("ContactNumber", null, "9890098900");
myXmlTextWriter.WriteStartElement("Department", null);
myXmlTextWriter.WriteElementString("Engineering", "SE");
myXmlTextWriter.WriteElementString("Management", "M2");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteElementString("Salary", "2000");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();
//Write the XML to file and close the myXmlTextWriter
myXmlTextWriter.Flush();
myXmlTextWriter.Close();
Console.WriteLine("Written to File");
}
It produces output as follows :
0 Comments:
Post a Comment
<< Home