Friday, October 10, 2008

Sample to Read & Write XML File through C#


Here is the sample to read and write xml file through C# code. In order to explain the process in a simple fashion, I made use of a xml file called SomeName.xml (The contents of the xml file are shown below in Section #1). Through C# code i am reading the nodes and attribute values which any one would prefer to do in their application. Then to explain the process of writing a xml file into file system, I am making use of the values I read from input xml file. I create a XmlDocument Object in memory, create nodes and with the read values append them inside and write them to a new XML file in file system called WrittenFile.xml.

Section #1 has the content of input xml file, Section #2 contains the c# code to be placed in pageload event & section #3 has the content of generated output xml file.

Section #1:

Input XML file: ~\XML\SomeFolder\SomeName.xml

<?xml version="1.0" standalone="yes" ?>




<column name="Attribute1" />

<column name="Attribute2" />



<row Attribute1="6693" Attribute2="655" />

<row Attribute1="432" Attribute2="364" />

<row Attribute1="34" Attribute2="766" />

<row Attribute1="665493" Attribute2="134" />

<row Attribute1="56783" Attribute2="656" />




Section #2:

C# Code written in page load event:

protected void Page_Load(object sender, EventArgs e)




//Creating XmlDocument Class object:

XmlDocument oXmlDocument = new XmlDocument();

//Loading the XML file from File System:


//1.To Read a Single Node

//Giving the Xpath of the Node: eg: //report/table/rows/row

//Assigning the returned XmlNoded from SelectSingleNode Method:

XmlNode oXmlElementCampaignName = oXmlDocument.DocumentElement.SelectSingleNode("//report/table/rows/row");

//2.To Read Collection of Nodes

//Giving the Xpath to method SelectNodes eg: SelectNodes("//report/table/rows/row");

//Assigning the returned XmlNoded from SelectSingleNode Method:

XmlNodeList oXmlNodeList = oXmlDocument.DocumentElement.SelectNodes("//report/table/rows/row");

// Create a XmlDocument object to write a file:

XmlDocument xmlDocument = new XmlDocument();

// Create a XmlElement which represents an Element : Root Element

XmlElement Items = xmlDocument.CreateElement("Items");

//Iterate through every nodes in oXmlNodeList which is a collection of Nodes

foreach (XmlNode objNode in oXmlNodeList)


// Create a XmlElement which represents an Element : Node

XmlElement Item = xmlDocument.CreateElement("Item");

// Create Child Node XmlElement1

XmlElement xmlelement1 = xmlDocument.CreateElement("XmlElement1");

// Sets value for Child Node

XmlText xmlelement1_text = xmlDocument.CreateTextNode(objNode.Attributes["Attribute1"].


// Placed inside Child Node


XmlElement xmlelement2 = xmlDocument.CreateElement("XmlElement2");

XmlText xmlelement2_text = xmlDocument.CreateTextNode(objNode.Attributes["Attribute2"].



// Placing under Node


// Placing under Node


// Placing under Root



// Placing under XML document


// Writing the XML file into file system



catch (Exception ex)


//Exception block here



Section #3:

Output XML file: ~\XML\SomeFolder\WrittenFile.xml























Hope this should be helpful for people who are looking to samples to read or write xml file.

Mohammed Fauzi

1 comment:

Pravesh Singh said...

Very nice article. I really enjoyed it reading. And it also cleared lot of my doubts about reading and writing XML in C#.Net, Well done job!
Check this link too....
How to read and write XML in C#

It helped me lot in completing my task.

Thanks Everyone for precious post!