Home Forums C# Programming Manipulate XML File Data Using C#

Viewing 0 reply threads
  • Author
    • #1922

      Manipulate XML File Data Using C#
      By Anand Narayanaswamy

      Nearly 95 percent of .NET applications use XML for various tasks. A main usage for Web developers is combining XML with HTML to display information on Web pages, which relieves them from having to spend a long time editing the content on their Web pages. A single change to an XML file will be reflected across the entire Web site, thus simplifying the development and also greatly reducing the development time.
      With the advent of .NET, programming languages such as C#, Visual Basic .NET, and frameworks such as ASP.NET took advantage of XML’s rich features. In fact, the configuration file (web.config), which is used in ASP.NET applications, is completely built upon XML tags.
      This article demonstrates how to manipulate an XML file using C#. 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


      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 display all the data using a C# console application (see Listing 2).
      Listing 2:

      DisplayCatalog.csXmlNodeList xmlnode = xmldoc.GetElementsByTagName("Book");
      Console.WriteLine("Here is the list of catalogsnn");
      for(int i=0;i
      //First Child of the XML file - Catalog.xml - returned
      //Example: Mark
      //Last Child of the XML file - Catalog.xml - returned
      //Example: Sams

      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.csFileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
      xmldoc = new XmlDocument();

      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("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
      xmldoc.DocumentElement.InsertAfter(newcatalogentry ,
      // 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
      • This topic was modified 7 months, 3 weeks ago by M. Saqib.
      • This topic was modified 7 months, 3 weeks ago by M. Saqib.
      • This topic was modified 7 months, 3 weeks ago by M. Saqib.

Viewing 0 reply threads
  • The forum ‘C# Programming’ is closed to new topics and replies.