faqts : Computers : Programming : Languages : C#

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

47 of 53 people (89%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How to create an XML file using C#.NET???

May 18th, 2005 00:47
Krishnan .L.N,


U can use the System.Xml namespace in the .NET Framework for working 
with xml.
Here to create a xml file,we are going to use the XmlTextWriter class.
It provides a way of creating files containing xml data....
Now let us go to the code.................
string filename="c:\\employee.xml";
XmlTextWriter tw=new XmlTextWriter(filename,null);//null represents 
the Encoding Type//
tw.Formatting=Formatting.Indented;  //for xml tags to be indented//
tw.WriteStartDocument();   //Indicates the starting of document
(Required)//
tw.WriteStartElement("Employees");			
tw.WriteStartElement("Employee","Genius");
tw.WriteAttributeString("Name","krishnan");
tw.WriteElementString("Designation","Software Developer");
tw.WriteElementString("FullName","krishnan Lakshmipuram Narayanan");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument();			
tw.Flush();
tw.Close();
When u execute the above code,U would be getting the following xml 
file generated....
<?xml version="1.0"?>
<Employees>
  <Employee Name="krishnan" xmlns="Genius">
    <Designation>Software Developer</Designation>
    <FullName>krishnan Lakshmipuram Narayanan</FullName>
  </Employee>
</Employees>