Skip to main content

Document Structure Basics

What is a DOCX File Really?​

A .docx file isn't a single fileβ€”it's actually a ZIP archive containing multiple XML files and media resources. Understanding this structure is fundamental to working with OOXML.

The Big Revelation​

πŸ’‘ Pro Tip: Rename any .docx file to .zip and open it with 7-Zip or WinRAR to explore its contents!


Exploring the Archive​

Try It Yourself (5-minute exercise)​

  1. Take any DOCX file
  2. Make a copy of it
  3. Rename the copy from document.docx to document.zip
  4. Open with 7-Zip, WinRAR, or Windows Explorer
  5. Browse the internal structure

You'll see something like this:

document.zip
β”œβ”€β”€ [Content_Types].xml
β”œβ”€β”€ _rels/
β”‚ └── .rels
β”œβ”€β”€ docProps/
β”‚ β”œβ”€β”€ app.xml
β”‚ └── core.xml
└── word/
β”œβ”€β”€ document.xml ← Main content here!
β”œβ”€β”€ styles.xml
β”œβ”€β”€ numbering.xml
β”œβ”€β”€ settings.xml
β”œβ”€β”€ fontTable.xml
β”œβ”€β”€ webSettings.xml
β”œβ”€β”€ _rels/
β”‚ └── document.xml.rels ← Relationships for images, etc.
β”œβ”€β”€ media/
β”‚ β”œβ”€β”€ image1.png
β”‚ └── image2.jpg
β”œβ”€β”€ header1.xml ← If document has headers
└── footer1.xml ← If document has footers

Key Files Breakdown​

πŸ“„ [Content_Types].xml​

Purpose: MIME type registry for all parts in the package

What it does: Tells Word what kind of content each file contains

Example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Default Extension="png" ContentType="image/png"/>
<Default Extension="jpeg" ContentType="image/jpeg"/>
<Override PartName="/word/document.xml"
ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
<Override PartName="/word/styles.xml"
ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
</Types>

⚠️ Common Pitfall: If you add a new part (like a header or image) but don't update this file, Word won't recognize it!


πŸ“ /_rels/ Folder​

Purpose: Contains relationship files that connect document parts

Key file: .rels - The root relationships file

What it connects:

  • Main document part (/word/document.xml)
  • Document properties (/docProps/)
  • Custom XML parts

Example .rels:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Target="word/document.xml"/>
<Relationship Id="rId2"
Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
Target="docProps/core.xml"/>
</Relationships>

πŸ”— See Also: Relationship Files for detailed explanation


πŸ“ /word/ Folder - The Heart of the Document​

This is where all the document content lives.

document.xml - THE MOST IMPORTANT FILE​

Purpose: Contains the actual document content (all text, tables, images references)

Structure:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:r>
<w:t>Hello World</w:t>
</w:r>
</w:p>
</w:body>
</w:document>

This simple example shows:

  • w:document - Root element
  • w:body - Document body container
  • w:p - Paragraph
  • w:r - Run (text with consistent formatting)
  • w:t - Text node (actual text content)

styles.xml - Style Definitions​

Purpose: Defines all paragraph, character, table, and numbering styles

Contains:

  • Built-in styles (Normal, Heading1, etc.)
  • Custom styles (your system's NormalPlaceholder, PropertyImagesTable, etc.)
  • Style hierarchy and inheritance

Example:

<w:style w:type="paragraph" w:styleId="Normal">
<w:name w:val="Normal"/>
<w:qFormat/>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
<w:sz w:val="24"/> <!-- 12pt font -->
</w:rPr>
</w:style>

πŸ”— See Also: Styles and Formatting

numbering.xml - List Definitions​

Purpose: Defines numbering formats for ordered and unordered lists

Why separate: Multiple paragraphs can reference the same numbering definition

Structure:

  • Abstract numbering definitions (templates)
  • Concrete numbering instances (actual usage)

πŸ”— See Also: Numbering and Lists

settings.xml - Document Settings​

Purpose: Document-level settings and configurations

Contains:

  • View settings (print view, web view)
  • Zoom level
  • Track changes settings
  • Compatibility settings
  • Default tab stop

Usually: Less critical for programmatic generation

fontTable.xml - Font Declarations​

Purpose: Lists all fonts used in the document

Example:

<w:font w:name="Times New Roman">
<w:panose1 w:val="02020603050405020304"/>
<w:charset w:val="00"/>
<w:family w:val="roman"/>
<w:pitch w:val="variable"/>
</w:font>

/word/media/ - Embedded Images​

Purpose: Stores all embedded images and media files

File naming: Usually image1.png, image2.jpeg, etc.

Referenced by: Relationship IDs in document.xml.rels

Formats supported:

  • PNG (.png)
  • JPEG (.jpg, .jpeg)
  • GIF (.gif)
  • SVG (.svg)
  • EMF/WMF (.emf, .wmf) - Vector formats

πŸ’‘ Pro Tip: Images are stored as-is (no compression). Large images = large DOCX files!

/word/_rels/document.xml.rels - Document Relationships​

Purpose: Maps relationship IDs to actual resources

What it links:

  • Images to image files
  • Hyperlinks to URLs
  • Headers/footers to their XML files
  • Footnotes/endnotes to their XML files
  • Numbering definitions
  • Styles

Example:

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
Target="media/image1.png"/>
<Relationship Id="rId2"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
Target="https://example.com"
TargetMode="External"/>
<Relationship Id="rId3"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"
Target="header1.xml"/>
</Relationships>

Headers and Footers​

If a document has headers or footers, you'll find them as separate XML files:

/word/
β”œβ”€β”€ header1.xml ← First page header
β”œβ”€β”€ header2.xml ← Even pages header
β”œβ”€β”€ header3.xml ← Odd pages header (default)
β”œβ”€β”€ footer1.xml ← First page footer
β”œβ”€β”€ footer2.xml ← Even pages footer
└── footer3.xml ← Odd pages footer (default)

How they work:

  • Each section can have different headers/footers
  • Referenced via section properties (w:sectPr) in document.xml
  • Linked through relationships in document.xml.rels

πŸ”— See Also: Advanced OOXML Internals


Document Properties (/docProps/)​

core.xml - Core Properties​

Purpose: Standard document metadata

Contains:

  • Title
  • Subject
  • Author
  • Keywords
  • Created date
  • Modified date
  • Description

Example:

<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties">
<dc:title>Appraisal Report</dc:title>
<dc:creator>AppraiSys System</dc:creator>
<dcterms:created>2025-11-03T12:00:00Z</dcterms:created>
<dcterms:modified>2025-11-03T13:30:00Z</dcterms:modified>
</cp:coreProperties>

app.xml - Application Properties​

Purpose: Application-specific metadata

Contains:

  • Application name (Microsoft Word)
  • Document template
  • Total editing time
  • Number of pages, words, characters
  • Company name

Visual Summary​


Why This Matters​

Understanding the DOCX structure helps you:

  1. Debug Issues: Know where to look when something breaks
  2. Understand Errors: "Relationship not found" = check .rels files
  3. Programmatic Generation: Know which files to create/modify
  4. Performance: Understand why large documents are slow (many XML parts)
  5. Manual Fixes: Edit XML directly to repair corrupted documents

Practical Example: Manual Text Edit​

Let's manually change text in a document:

Steps:

  1. Copy sample.docx to sample.zip
  2. Extract the ZIP
  3. Open /word/document.xml in a text editor
  4. Find <w:t>Original Text</w:t>
  5. Change to <w:t>New Text</w:t>
  6. Save the file
  7. Re-ZIP all contents (important: ZIP the contents, not the folder!)
  8. Rename back to .docx
  9. Open in Word

Result: Text changed without opening Word!

⚠️ Common Pitfall: If you ZIP the folder instead of its contents, Word won't recognize the structure. The [Content_Types].xml must be at the root of the ZIP.


How AppraiSys Uses This Structure​

In the AppraiSys codebase, you'll see:

Reading Documents​

// Using DocumentFormat.OpenXml SDK
using var wordDoc = WordprocessingDocument.Open(stream, true);
var mainPart = wordDoc.MainDocumentPart;
var document = mainPart.Document;
var body = document.Body;

Creating Documents​

// From DefaultDocument.cs
var wordDocument = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);
var mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document(new Body());

Working with Relationships​

// Adding an image
var imagePart = mainPart.AddImagePart(ImagePartType.Png);
imagePart.FeedData(imageStream);
string relationshipId = mainPart.GetIdOfPart(imagePart);

πŸ”— See Also: Creating Documents Code Examples


Quick Reference: File Locations​

Content TypeFile LocationPurpose
Main content/word/document.xmlAll document content
Styles/word/styles.xmlStyle definitions
Lists/word/numbering.xmlNumbering formats
Images/word/media/Embedded images
Headers/word/header*.xmlPage headers
Footers/word/footer*.xmlPage footers
Relationships/word/_rels/document.xml.relsResource links
Metadata/docProps/core.xmlDocument properties

Next Steps​

Now that you understand the physical structure of DOCX files, learn how the parts connect:

➑️ Next: Relationship Files - How document parts link together

Also Important:


Test Your Understanding​

Question 1​

What happens if you add an image to /word/media/ but don't create a relationship in document.xml.rels?

Answer

The image won't appear in the document. Word needs the relationship to know that the image exists and how to reference it from the content.

Question 2​

Can you have a valid DOCX file without a styles.xml file?

Answer

Technically yes, but Word will use default styles. In practice, almost all documents have a styles.xml file defining at least the "Normal" style.

Question 3​

What's the minimum set of files needed for a valid DOCX?

Answer

Minimum valid DOCX:

  • [Content_Types].xml
  • _rels/.rels
  • /word/document.xml
  • /word/_rels/document.xml.rels (can be empty)

However, Word will add more files when you save.


πŸ”§ Tools for Exploring DOCX Structure​

Open XML SDK Productivity Tool​

  • Download: GitHub Releases
  • Features: Visual tree view, validation, C# code generation
  • Best for: Inspecting structure without manually unzipping

7-Zip / WinRAR​

  • Best for: Quick extraction and exploration
  • Tip: Set to show XML files with syntax highlighting

Oxygen XML Editor​

  • Best for: Advanced XML editing with validation
  • Features: Schema validation, XPath testing
  • Note: Commercial tool (optional)

πŸ’‘ Remember: A DOCX is just a ZIP file with XML inside. Once you understand this, the "magic" disappears and you can work with confidence!