Join | Login  

     Popular! Books, Stories, Articles, Poetry
Where Authors and Readers come together!


Featured Authors:  Tonya Kinzer, iR.E.E.S Blogs Linda Smith, Publisher, iJackie Miller, iTom Kitt, iGracie McKeever, iG. Rynk, iKellee Stone, i

  Home > Technology > Articles Popular: Books, Stories, Articles, Poetry     

Anand Narayanaswamy

· Become a Fan
· Contact me
· Books
· Articles
· News
· 2 Titles
· 5 Reviews
· Save to My Library
· Share with Friends!
Member Since: Mar, 2007

Anand Narayanaswamy, click here to update your pages on AuthorsDen.

Featured Book
Love: Its Wonderments, Sufferings and Consumations (Free Aud
by Lonnie Hicks

Love stories, love poems, inspirational in nature are the core of this volume. Again, taken from individuals around the world, and of course, the authors own contribution..  
BookAds by Silver
Gold and Platinum Members

Featured Book
Children Of The Second Birth how lives were transformed
by Tuchy (Carl) Palmieri

The book "Children of the Second Birth" relays the stories of men and women that found God in times of despair and desperation. You learn of personal sacrifices and obsta..  
BookAds by Silver
Gold and Platinum Members

Working with XML using C#
by Anand Narayanaswamy   
Rated "G" by the Author.
Last edited: Sunday, March 18, 2007
Posted: Sunday, March 18, 2007

Share    Print   Save    Become a Fan

eXtensible Markup Language (XML) is one of the powerful language for representing data over the web. It is easy to understand and is also platform independent. Nearly 95% of .NET applications make use of XML for performing various tasks.

The language can be easily combined with HTML for displaying information on a webpage. With XML, developers need not have to spend long time for editing the content on their web pages. A single change to an XML file will be reflected across the whole of the web site thus simplifying the development and also greatly reduces the developerís time.

In this article, you will learn how to manipulate an XML file using C# programming language. Manipulation includes displaying, adding, editing and deleting data from a single XML file using C#. You will also learn how to make use of Stream class included with System.IO namespace and various other XML classes included with System.XML namespace. For the purpose of this article, I have created an XML file as shown in listing 1.1.

Listing 1.1



You should test the above XML file for any errors by simply opening the file using your browser. If there are no errors then the XML file will be displayed as such.

We will display all the data from the above XML file using a C# console application (See Listing 1.2)

XmlNodeList xmlnode = xmldoc.GetElementsByTagName("Book");
Console.WriteLine("Here is the list of catalogs\n\n");

for(int i=0;i {
XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;

//XML Attribute Name and Value returned


//First Child of the XML file - Catalog.xml - returned
//Example: Mark


//Last Child of the XML file - Catalog.xml - returned
//Example: Sams


The above listing is just an extract from the C# program. The code pulls the data from our XML file. It makes use of XMLNodeList class to retrieve the relevant XML node and then iterates it with the help of for loop and the class Count property. Inside the loop an instance of XMLAttributeCollection class is created and the appropriate values are displayed using the properties of the class.

Inside the constructor an instance of FileStream class is created (See Listing 1.3) and the required permissions are set. The XML document is loaded with the help of XMLDocument class and the required instance of FileStream class is loaded with Load() method of XMLDocument class. DisplayCatalog() method is the one which we discussed in listing 1.2

Listing 1.3

FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
xmldoc = new XmlDocument();

You can download complete source code for all the examples covered on this article from the code download section at the end of this article.

The final output looks like as shown in figure 1.1

Adding Data to an XML file using C# application

The code given below appends a new catalog entry to the XML document. It uses the various properties and methods of the XMLDocument class. Initially, a new XML element and attribute are created. The attribute is then attached to the XML element with the help of SetAttributeNode method. We then create the first and second elements and the require values are supplied by using the InnerText property. The newly created elements are appended to the XML file by using AppendChild method. After the above mentioned process, the new XML element is inserted into the file with the help of appropriate methods.

// New XML Element Created
XmlElement newcatalogentry = xmldoc.CreateElement("Book");

// New Attribute Created
XmlAttribute newcatalogattr = xmldoc.CreateAttribute("ID");

// Value given for the new attribute
newcatalogattr.Value = "005";

// Attach the attribute to the xml element

// First Element - Book - Created
XmlElement firstelement = xmldoc.CreateElement("Book");

// Value given for the first element
firstelement.InnerText = "Peter";

// Append the newly created element as a child element

// Second Element - Publisher - Created
XmlElement secondelement = xmldoc.CreateElement("Publisher");

// Value given for the second element
secondelement.InnerText = "Que Publishing";

// Append the newly created element as a child element

// New XML element inserted into the document

// An instance of FileStream class created
// First parameter is the path to our XML file - Catalog.xml

FileStream fsxml = new FileStream(path,FileMode.Truncate,FileAccess.Write,FileShare.ReadWrite);

// XML Document Saved

As you can see from the above code, the new child node gets inserted after the last child node. It can be added before the last child node using InsertBefore method of XMLDocument class. Finally, an instance of FileStream class is created and the document is saved using Save() method of XMLDocument class.

Reader Reviews for "Working with XML using C#"

Want to review or comment on this article?
Click here to login!

Need a FREE Reader Membership?
Click here for your Membership!

Reviewed by Mary Coe 6/25/2007
You are so knowledgeable. You look so young. Great article. Very interesting read.
Reviewed by Sandie May Angel-Joyce 5/22/2007
You are so knowldgeable!

Sandie Angel :o)
Reviewed by m j hollingshead 3/19/2007
interesting article

Forced Hot Air Furnace - Troubleshooting and Repair by Roger Vizi

Complete Troubleshooting & Repairing guide to hot air furnaces * Complete operation, maintenance, and repair * Covers gas, oil, and electric forced air systems * Includes flowchart..  
Featured BookAds by Silver
Gold and Platinum Members

Forced Hot Air Furnace - Troubleshooting and Repair by Roger Vizi

Complete Troubleshooting & Repairing guide to hot air furnaces * Complete operation, maintenance, and repair * Covers gas, oil, and electric forced air systems * Includes flowchart..  
Featured BookAds by Silver
Gold and Platinum Members

Authors alphabetically: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Featured Authors | New to AuthorsDen? | Add AuthorsDen to your Site
Share AD with your friends | Need Help? | About us

Problem with this page?   Report it to AuthorsDen
© AuthorsDen, Inc. All rights reserved.