faqts : Computers : Programming : Languages : Xml

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

107 of 133 people (80%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How to create xml file using c#?

May 27th, 2005 17:36
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>