# Open and Compare the XML Contents of Word Documents (.docx)

## Environment

<table>
    <tbody>
        <tr>
            <th scope="row">Product Version</th>
            <td>2024.2.524</td>
        </tr>
        <tr>
            <th scope="row">Product</th>
            <td>Telerik UI for WPF</td>
        </tr>
    </tbody>
</table>

## Description

How to get the XML of Word (.docx) documents and compare their contents using Visual Studio.

## Solution

The .docx file format is an archive that can be unzipped. This gives several folders and files with the document contents, styles and other settings. To access the content, you can find and open the **document.xml** file. Then, you can compare the text in the **document.xml** files of the corresponding files.

This article describes how to do this using the [Open XML Package Editor for Modern Visual Studios](https://marketplace.visualstudio.com/items?itemName=bsivanov.OpenXMLPackageEditorforVisualStudio) extension tool for easier access to the files of the .docx file, without the need to unzip it. Visual Studio built-in diff tool can be used to compare the extracted XML contents.

1. Install the [Open XML Package Editor for Modern Visual Studios](https://marketplace.visualstudio.com/items?itemName=bsivanov.OpenXMLPackageEditorforVisualStudio) Visual Studio extension. This will allow you to open the **.docx** files content directly in Visual Studio.

2. [Create a new Blank Solution](https://learn.microsoft.com/en-us/visualstudio/get-started/tutorial-projects-solutions?view=vs-2022#create-a-solution) in Visual Studio. This is required in order to use the Visual Studio's [Compare Selection](https://learn.microsoft.com/en-us/visualstudio/ide/compare-with?view=vs-2022) feature.

3. Include the two **.docx** files that will be compared in the solution.

   ![A picture showing the Visual Studio Add Existing Item menu option](https://www.telerik.com/products/wpf/documentation/assets/6af2a4aa6996c37c0f753b35fe15ff21/kb-common-compare-docx-xml-content-0.png)\
   ![A picture showing two Word documents added in the Visual Studio solution](https://www.telerik.com/products/wpf/documentation/assets/b52d0bc6088ac86bfa7012565542466a/kb-common-compare-docx-xml-content-1.png)

4. Create two new empty **.xml** files in the solution.

   ![A picture showing the Visual Studio Add New Item menu option](https://www.telerik.com/products/wpf/documentation/assets/9918c0bdcd35b0d3b041b7a2aefe98c9/kb-common-compare-docx-xml-content-2.png)
   ![A picture showing the New File window in Visual Studio with the XML File selected](https://www.telerik.com/products/wpf/documentation/assets/e82557b5c390295fbaf1f6509a6c227b/kb-common-compare-docx-xml-content-3.png)
   ![A picture showing the two empty XML files added in the Visual Studio solution](https://www.telerik.com/products/wpf/documentation/assets/911c56f51e9d1e7b6b4520a1388f09c1/kb-common-compare-docx-xml-content-4.png)

5. Double click the first **.docx** file in the solution and open the **/word/documents.xml** file.

   ![A picture showing the contents of the docx file opened by the Open XML Package Editor extension](https://www.telerik.com/products/wpf/documentation/assets/1a9875224591f63e7af994ef5e7cc18c/kb-common-compare-docx-xml-content-5.png)
   ![A picture showing the contents of the documents.xml file](https://www.telerik.com/products/wpf/documentation/assets/f2e3281bcfa336733b5b35ab33f649b1/kb-common-compare-docx-xml-content-6.png)

   If the file is not formatted you can do that by using the **Edit-->Advanced-->Format Selection** option. The default shortcut for this action is **Ctrl+K, Ctrl+F**.

   ![A picture showing the Format Selection menu option](https://www.telerik.com/products/wpf/documentation/assets/82cd54e289cd0bb548d225ed80aac999/kb-common-compare-docx-xml-content-7.png)

6. Copy the content of the **documents.xml** file to one of the empty **.xml** files and save it.

   ![A picture showing the documents.xml contents moved in the newly created xml file](https://www.telerik.com/products/wpf/documentation/assets/ce17d75726f044e23eed4e86af935d99/kb-common-compare-docx-xml-content-8.png)

7. Repeat **step 5** also for the other **.docx** file and copy its XML contents to the other empty **.xml** file, and then save it.

8. Select the **.xml** files in the Solution Explorer and right click on any of the files to display the context menu. From the menu select the [Compare Selection](https://learn.microsoft.com/en-us/visualstudio/ide/compare-with?view=vs-2022) option.

   ![A picture showing the Compare Selection menu option in Visual Studio](https://www.telerik.com/products/wpf/documentation/assets/dc0c409b5a759cd65075bacdbb7ff02b/kb-common-compare-docx-xml-content-9.png)

This will open a new diff view showing the comparison between the two **.xml** files

![A picture showing the diff tool of Visual Studio comparing the two .xml files](https://www.telerik.com/products/wpf/documentation/assets/4b852628d04221656a86176a4b879143/kb-common-compare-docx-xml-content-10.png)
