DEV Community

Gia
Gia

Posted on

How to Save .DOCX as .XML using C#

XML is a universal markup language used to store and transfer data. This document format consists of tags, elements, attributes, and text. Converting Word documents to XML format allows you to express the content, format, and structure of the document in the form of tags, allowing for more flexible data processing and exchange.

Tool:

  • Visual Studio 2022
  • Free Spire.Doc for .NET This is totally free library, which enables developers to create, edit or convert Word documents on .NET platforms easily.

Installation:

  • Download and install Free Spire.Doc for .NET.
  • Create a new C# project in Visual Studio and open it.
  • Right-click " References " in the " Solution Explorer ".
  • And then select " Add Reference " > " Browse ".
  • Find the DLL file in the BIN folder under the installation path and click " OK " to add it as a reference.

Sample Codes:

After importing DLL files to your project, you can refer to the following codes to save Word document as XML file.

using System;
using Spire.Doc;

namespace WordtoXML
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document from disk
            document.LoadFromFile(@"sample.docx");

            //Save the Word document as an XML file
            document.SaveToFile("toXML.xml", FileFormat.Xml);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This method also supports converting Word document to other file format, such as XPS, PDF, HTML.

Image description

Top comments (0)