How to Write XML Into a File in C#

Muhammad Zeeshan Feb 02, 2024
  1. The XML Format
  2. Write XML Into a File Using the XmlWriter Class in C#
How to Write XML Into a File in C#

This tutorial will teach us how to utilize the C# programming language to put XML into a file. First, look at the format of an XML file and see how it’s organized.

The XML Format

XML, which stands for Extensible Markup Language, is a markup language similar to HTML but distinguishable in that it uses tags to define objects. The XML Document Object Model (DOM), an application programming interface for both HTML and XML documents, is the foundation for the XML file format.

A standard mechanism to access and alter the XML document components is defined by the XML Document Object Model (DOM). It creates a tree-structure representation of an XML document, which may be used to access all of the document’s components through the DOM tree.

The XML tree allows for modifying or deleting previously produced elements and creating new components. A node is a name for each element that makes up an XML document.

Write XML Into a File Using the XmlWriter Class in C#

The XmlWriter class is responsible for adding text to XML files and has the necessary capabilities. It is an abstract foundation class from which the XmlTextWriter and XmlNodeWriter classes derive their functionality.

It has a variety of methods and attributes that may be used to create XML documents. This class has many Writer methods, one for each kind of item that may be found in an XML document.

Some of them include WriteNode, WriteString, WriteAttributes, WriteStartElement, and WriteEndElement. Other examples are WriteAttributes and WriteStartElement.

Some of these procedures are used in conjunction with one another to form a start and finish pair. For instance, to write an element, you must first use the WriteStartElement method, after which you must write a string and then call the WriteEndElement method.

For better understanding, let’s have a look at an example of writing XML using the XmlWriter class:

  1. To begin, we must import the following libraries:

    using System;
    using System.Text;
    using System.Xml;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
  2. In the Main() class, we will construct an object of XMLWriter and name it xW. We will then supply the path and name of the XML file as an argument to the XMLWriter object.

    XmlWriter xW = XmlWriter.Create("D:/myarticle/xmlfilebyZeeshan.xml");
    
  3. We will begin creating the XML file with the help of the WriteStartDocument() function.

    xW.WriteStartDocument();
    
  4. We will start writing the first element using the WriteStartElement() function. WriteAttributeString("CGPA", "3.6") and WriteString("Muhammad Zeeshan") will be used to assign various data values, and the element will be ended once WriteEndElement() is called.

    xW.WriteStartElement("First");
    xW.WriteAttributeString("CGPA", "3.6");
    xW.WriteString("Muhammad Zeeshan");
    xW.WriteEndElement();
    
  5. Then, to finish the file, we will use the below functions to write the second and third elements.

    xW.WriteStartElement("Second");
    xW.WriteAttributeString("CGPA", "3.9");
    xW.WriteString("Haseeb ur Rehman");
    xW.WriteEndElement();
    
    xW.WriteStartElement("Third");
    xW.WriteAttributeString("CGPA", "3.8");
    xW.WriteString("Bawa Younus");
    xW.WriteEndElement();
    
  6. After we have finished writing all of the data, we will end the document using the WriteEndDocument() function, and then we will use the Close() function to close the XMLWriter.

    xW.WriteEndDocument();
    xW.Close();
    

Complete Source Code

using System;
using System.Text;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace writexmlbyZeeshan {
  class Program {
    static void Main(string[] args) {
      XmlWriter xW = XmlWriter.Create("D:/myarticle/xmlfilebyZeeshan.xml");

      xW.WriteStartDocument();
      xW.WriteStartElement("Students");

      xW.WriteStartElement("First");
      xW.WriteAttributeString("CGPA", "3.6");
      xW.WriteString("Muhammad Zeeshan");
      xW.WriteEndElement();

      xW.WriteStartElement("Second");
      xW.WriteAttributeString("CGPA", "3.9");
      xW.WriteString("Haseeb ur Rehman");
      xW.WriteEndElement();

      xW.WriteStartElement("Third");
      xW.WriteAttributeString("CGPA", "3.8");
      xW.WriteString("Bawa Younus");
      xW.WriteEndElement();

      xW.WriteEndDocument();
      xW.Close();
    }
  }
}

Output:

<Students>
<First CGPA="3.6">Muhammad Zeeshan</First>
<Second CGPA="3.9">Haseeb ur Rehman</Second>
<Third CGPA="3.8">Bawa Younus</Third>
</Students>
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Csharp XML

Related Article - Csharp File