WordprocessingML 文件的組織結構 (C#)Shape of WordprocessingML Documents (C#)

這個主題說明 WordprocessingML 文件的 XML 組織結構。This topic introduces the XML shape of a WordprocessingML document.

Microsoft Office 格式Microsoft Office Formats

2007 Microsoft Office 系統的原始檔案格式為 Office Open XML (一般稱為 Open XML)。The native file format for the 2007 Microsoft Office system is Office Open XML (commonly called Open XML). Open XML 是一種 Ecma 標準,而且目前正在通過 ISO-IEC 標準程序的 XML 格式。Open XML is an XML-based format that an Ecma standard and is currently going through the ISO-IEC standards process. 在 Open XML 中,字組處理檔案的標記語言稱為 WordprocessingML。The markup language for word processing files within Open XML is called WordprocessingML. 這個教學課程會使用 WordprocessingML 原始程式檔做為範例的輸入。This tutorial uses WordprocessingML source files as input for the examples.

如果您是使用 Microsoft Office 2003 且有安裝 Word、Excel 與 PowerPoint 2007 檔案格式的 Microsoft Office 相容性套件,便可以將文件儲存為 Office Open XML 格式。If you are using Microsoft Office 2003, you can save documents in the Office Open XML format if you have installed the Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats.

WordprocessingML 文件的組織結構The Shape of WordprocessingML Documents

第一件要了解的事情是 WordprocessingML 文件的組織結構。The first thing to understand is the shape of WordprocessingML documents. WordprocessingML 文件包含具有文件段落的本文項目 (稱為 w:body)。A WordprocessingML document contains a body element (named w:body) that contains the paragraphs of the document. 每個段落都包含一或多個文字執行 (稱為 w:r)。Each paragraph contains one or more text runs (named w:r). 每個文字執行都包含一或多個文字片段 (稱為 w:t)。Each text run contains one or more text pieces (named w:t).

下列是非常簡單的 WordprocessingML 文件:The following is a very simple WordprocessingML document:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>  
    <w:p w:rsidR="00E22EB6"  
        <w:t>This is a paragraph.</w:t>  
    <w:p w:rsidR="00E22EB6"  
        <w:t>This is another paragraph.</w:t>  

這份文件包含兩個段落。This document contains two paragraphs. 這兩個段落都包含單一的文字執行,而且每個文字執行都包含單一的文字片段。They both contain a single text run, and each text run contains a single text piece.

查看 XML 格式之 WordprocessingML 文件內容最簡單的方式是使用 Microsoft Word 建立一個這種文件、儲存起來,然後執行下列程式,將 XML 列印到主控台。The easiest way to see the contents of a WordprocessingML document in XML form is to create one using Microsoft Word, save it, and then run the following program that prints the XML to the console.

這個範例會使用在 WindowsBase 組件中找到的類別。This example uses classes found in the WindowsBase assembly. 它會使用 System.IO.Packaging 命名空間中的型別。It uses types in the System.IO.Packaging namespace.

const string documentRelationshipType =  
const string wordmlNamespace =  
XNamespace w = wordmlNamespace;  
using (Package wdPackage = Package.Open("SampleDoc.docx", FileMode.Open, FileAccess.Read))  
    PackageRelationship relationship =  
    if (relationship != null)  
        Uri documentUri =  
                new Uri("/", UriKind.Relative),  
        PackagePart documentPart = wdPackage.GetPart(documentUri);  
        //  Get the officeDocument part from the package.  
        //  Load the XML in the part into an XDocument instance.  
        XDocument xdoc =  

外部資源External Resources

Introducing the Office (2007) Open XML File Formats (Office (2007) Open XML 檔案格式簡介)Introducing the Office (2007) Open XML File Formats
Overview of WordprocessingML (WordprocessingML 概觀)Overview of WordprocessingML
WordProcessingML 檔案的結構 (英文)Anatomy of a WordProcessingML File
WordprocessingML 簡介 (英文)Introduction to WordprocessingML
Office 2003:XML 參考結構描述下載頁面Office 2003: XML Reference Schemas Download page

另請參閱See also