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)β
- Take any DOCX file
- Make a copy of it
- Rename the copy from
document.docxtodocument.zip - Open with 7-Zip, WinRAR, or Windows Explorer
- 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 elementw:body- Document body containerw:p- Paragraphw: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) indocument.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:
- Debug Issues: Know where to look when something breaks
- Understand Errors: "Relationship not found" = check
.relsfiles - Programmatic Generation: Know which files to create/modify
- Performance: Understand why large documents are slow (many XML parts)
- Manual Fixes: Edit XML directly to repair corrupted documents
Practical Example: Manual Text Editβ
Let's manually change text in a document:
Steps:
- Copy
sample.docxtosample.zip - Extract the ZIP
- Open
/word/document.xmlin a text editor - Find
<w:t>Original Text</w:t> - Change to
<w:t>New Text</w:t> - Save the file
- Re-ZIP all contents (important: ZIP the contents, not the folder!)
- Rename back to
.docx - 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 Type | File Location | Purpose |
|---|---|---|
| Main content | /word/document.xml | All document content |
| Styles | /word/styles.xml | Style definitions |
| Lists | /word/numbering.xml | Numbering formats |
| Images | /word/media/ | Embedded images |
| Headers | /word/header*.xml | Page headers |
| Footers | /word/footer*.xml | Page footers |
| Relationships | /word/_rels/document.xml.rels | Resource links |
| Metadata | /docProps/core.xml | Document 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:
- WordprocessingML Core - Understanding the XML elements
- Element Hierarchy Rules - Valid structure rules
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!