October 1, 2020
Hot Topics:

Manipulate XML File Data Using C#

  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

XML is used for nearly 95 percent of .NET applications for various tasks. Combining XML with HTML to display information on Web pages, relieves Web developers from having to spend lots of time editing the content on their Web pages. An easy change to an XML file can be reflected across the entire Web site, thus reducing development time and simplifying overall development.

C#, Visual Basic .NET, and other .NET programmign langauges, along with frameworks such as ASP.NET, all take advantage of XML's rich features. In fact, ASP.NET' application's configuration file (web.config) is built completely upon XML tags.

In this article, you will learn how to use C# to manipulate an XML file. The manipulation includes displaying, adding, editing, and deleting data from a single XML file using C#. It also shows how to use the Stream class included in the System.IO namespace and various other XML classes included in the System.XML namespace.

Display Contents of XML File

Listing 1 shows a simple XML file for demonstration:

Listing 1

<?xml version="1.0"?>
<Book ID="001">

<Book ID="002">

To test the above XML file for errors, simply open it with your browser. If it has no errors, the file will be displayed as such.

The next step is to use C# to display all of the data in a console application (see Listing 2).

Listing 2: A Console application to display XML (DisplayCatalog.cs)

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

for(int i=0;i<xmlnode.Count;i++)
XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;

//XML Attribute Name and Value returned
//Example: <Book id = "001">


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


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


Listing 2 is just an extract from the DisplayCatalog() method of the C# application. It displays the data from the XML file. It uses the XMLNodeList class to retrieve the relevant XML node and then iterates it with the help of the for loop and the Count property of the class. Inside the loop, it creates an instance of the XMLAttributeCollection class and displays the appropriate values using the properties of the class.

Inside the constructor, the code creates an instance of the FileStream class and sets the required permissions (see Listing 3). It then loads the XML document with the help of the XMLDocument class and loads the required instance of the FileStream class with the Load() method of the XMLDocument class.

Listing 3: DisplayCatalog.cs

FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,
xmldoc = new XmlDocument();
You can download the complete source code for all the from the code download section at the end of this article.

The final output looks like the display in Figure 1.

Figure 1: Final Output from Listings 2 and 3

The above walkthrough showed how to display the contents of an XML file using a C# program. The next section demonstrates how to write data directly to the XML file using a C# console application.

Write Data Directly to XML File

Listing 4 appends a new catalog entry to the XML document using the various properties and methods of the XMLDocument class.

Listing 4: AddCatalog.cs

// 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("Author");

// 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
// The first parameter is the path to the XML file - Catalog.xml

FileStream fsxml = new FileStream(path,FileMode.Truncate,

// XML Document Saved

Page 1 of 2

This article was originally published on March 14, 2005

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date