RSS

Write Data in XML File Using C#

07 Jan

Sample XML File

<?xml version="1.0" encoding="utf-8"?>

<DoctorInfo>

&nbsp;

<Doctor>

<id>01</id>

<name>Ranil Jayaweera</name>

<speciality>GI</speciality>

</Doctor>

&nbsp;

<Doctor>

<id>02</id>

<name>Aruna Gunathilaka</name>

<speciality>VP</speciality>

</Doctor>

&nbsp;

<Doctor>

<id>03</id>

<name>Jaya Palipana</name>

<speciality>VOG</speciality>

</Doctor>

&nbsp;

</DoctorInfo>

The Code

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(Application.StartupPath + "\\DoctorsList.xml");

XmlElement subroot = xmlDoc.CreateElement("Doctor");

&nbsp;

XmlElement childElementId = xmlDoc.CreateElement("id");

XmlText xmlTextId = xmlDoc.CreateTextNode(txtid.Text);

childElementId.AppendChild(xmlTextId);

subroot.AppendChild(childElementId);

xmlDoc.DocumentElement.AppendChild(subroot);

&nbsp;

XmlElement childElementName = xmlDoc.CreateElement("name");

XmlText xmlTextName = xmlDoc.CreateTextNode(txtName.Text);

childElementName.AppendChild(xmlTextName);

subroot.AppendChild(childElementName);

xmlDoc.DocumentElement.AppendChild(subroot);

&nbsp;

XmlElement childElementSpeciality = xmlDoc.CreateElement("speciality");

XmlText xmlTextSpeciality = xmlDoc.CreateTextNode(cbspecial.Text);

childElementSpeciality.AppendChild(xmlTextSpeciality);

subroot.AppendChild(childElementSpeciality);

xmlDoc.DocumentElement.AppendChild(subroot);

&nbsp;

xmlDoc.Save(Application.StartupPath + "\\DoctorsList.xml");
Advertisements
 
Leave a comment

Posted by on January 7, 2012 in C#.Net, XML

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: