C# – Write and Read XML

About XML

XML (Extensible Markup Language) is one kind of source language, which allows users defining their own markup language. It can be used to mark electronic files to make these files become markup language with structures. Also, it can mark data and define data type.

The main feature of XML is to store data but it is quite different from database. XML just displays data. Because it is very simple and convenient for delivering on Web, XML file is widely used.

Write & Read XML

In this post, I will talk something about how to write and read XML by using C# via Spire.XLS.

At the beginning, add necessary references, Spire.XLS dll file, System.Windows.Forms and System.IO.

Write XML:

  1. Create a new workbook and initialize worksheet.
  2. Write contents in this workbook and then set format for contents.
  3. Auto fit column and set row height for cells which are filled with data.
  4. Save file to XML and launch it.


using System;

using System.IO;

using System.Windows.Forms;

using Spire.Xls;

namespace WritenReadXML


    class XML


        static void Main(string[] args)


            Workbook workbook = new Workbook();


            Worksheet sheet = workbook.Worksheets[0];

            //Write Contents

            sheet.Range[“C10”].Text = “WriteXML”;

            sheet.Range[“C10”].Style.Font.FontName = “Calibari”;

            sheet.Range[“C10”].Style.Font.Size = 14;

            sheet.Range[“C10”].Style.KnownColor = ExcelColors.LightBlue;

            sheet.Range[“C11”].Text = “ReadXML”;

            sheet.Range[“C11”].Style.Font.FontName = “Calibari”;

            sheet.Range[“C11”].Style.Font.Size = 14;

            sheet.Range[“C11”].Style.KnownColor = ExcelColors.YellowCustom;

            //Autofit Column and Set Row Hight


            sheet.SetRowHeight(10, 20);

            sheet.SetRowHeight(11, 20);

            //Save as XML


            //Launch XML

            System.Diagnostics.Process.Start(Path.Combine(Application.StartupPath, “WriteXML.xml”));

Open with Excel:

Open with Text:

Read XML:

Now, I want to read the xml file which I wrote right now.  Firstly, I save this file to specified path and read it.

  1. Create workbook and initialize worksheet.
  2. Load xml file from my computer.
  3. Save this xml file as Excel.
  4. Launch file.


            Workbook workbook = new Workbook();

            Worksheet sheet = workbook.Worksheets[0];

            using (FileStream fileStream = File.OpenRead(@”D:\work\My Documents\WriteXML.Xml”))







Click Here to learn more about Spire.XLS

Click Here to download Spire.XLS


One thought on “C# – Write and Read XML

  1. Abhinav Bindra says:

    Informative article !!! it gives a basic knowledge of reading and writing XML file using C# code. I’ve found an other post related to this over internet during searching time article over internet. It also explained very well, check out this url for more information of this post…..


    Lastly! I say to thanks everyone for their precious post!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s