Friday, October 10, 2008

Sample to Read & Write XML File through C#

Hi,

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" ?>

<report>

<table>

<columns>

<column name="Attribute1" />

<column name="Attribute2" />

</columns>

<rows>

<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" />

</rows>

</table>

</report>

Section #2:

C# Code written in page load event:

protected void Page_Load(object sender, EventArgs e)

{

try

{

//Creating XmlDocument Class object:

XmlDocument oXmlDocument = new XmlDocument();

//Loading the XML file from File System:

oXmlDocument.Load(Server.MapPath("XML/SomeFolder/SomeName.xml"));

//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"].

Value.ToString());

// Placed inside Child Node

xmlelement1.AppendChild(xmlelement1_text);

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

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

Value.ToString());

xmlelement2.AppendChild(xmlelement2_text);

// Placing under Node

Item.AppendChild(xmlelement1);

// Placing under Node

Item.AppendChild(xmlelement2);

// Placing under Root

Items.AppendChild(Item);

}

// Placing under XML document

xmlDocument.AppendChild(Items);

// Writing the XML file into file system

xmlDocument.Save(Server.MapPath("XML/SomeFolder/WrittenFile.xml"));

}

catch (Exception ex)

{

//Exception block here

}

}

Section #3:

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

<Items>

<Item>

<XmlElement1>6693</XmlElement1>

<XmlElement2>655</XmlElement2>

</Item>

<Item>

<XmlElement1>432</XmlElement1>

<XmlElement2>364</XmlElement2>

</Item>

<Item>

<XmlElement1>34</XmlElement1>

<XmlElement2>766</XmlElement2>

</Item>

<Item>

<XmlElement1>665493</XmlElement1>

<XmlElement2>134</XmlElement2>

</Item>

<Item>

<XmlElement1>56783</XmlElement1>

<XmlElement2>656</XmlElement2>

</Item>

</Items>

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

Regards
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!