本文介紹 WordprocessingML 檔的 XML 圖形。
Microsoft Office 格式
2007 Microsoft Office 系統的原生檔格式是 Office Open XML(通常稱為 Open XML)。 Open XML 是以 XML 為基礎的格式,它是 Ecma 標準,目前正在進行 ISO-IEC 標準化流程。 Open XML 中文字處理檔案的標記語言稱為 WordprocessingML。 本教學課程使用 WordprocessingML 來源檔案作為範例的輸入。
如果您使用 Microsoft Office 2003,如果您已安裝適用於 Word、Excel 和 PowerPoint 2007 檔格式的 Microsoft Office 兼容性套件,則可以以 Office Open XML 格式儲存檔。
WordprocessingML 檔的形狀
首先要瞭解的是 WordprocessingML 檔的 XML 圖形。 WordprocessingML 文件包含主體元素(名為 w:body),其中包含文件的段落。 每個段落都包含一或多個文字段(名為 w:r)。 每個文字執行都包含一或多個文字片段(名為 w:t)。
以下是非常簡單的 WordprocessingML 檔:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:document
xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
<w:body>
<w:p w:rsidR="00E22EB6"
w:rsidRDefault="00E22EB6">
<w:r>
<w:t>This is a paragraph.</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00E22EB6"
w:rsidRDefault="00E22EB6">
<w:r>
<w:t>This is another paragraph.</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
本檔包含兩個段落。 兩者都包含單一文字執行,而每個文字執行都包含單一文字片段。
若要以 XML 格式查看 WordprocessingML 檔的內容,最簡單的方式是使用 word Microsoft 建立檔、儲存它,然後執行下列程式,將 XML 列印至控制台。
此範例使用在 WindowsBase 元件中找到的類別。 它會使用 命名空間中的 System.IO.Packaging 類型。
const string documentRelationshipType =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
const string wordmlNamespace =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace w = wordmlNamespace;
using (Package wdPackage = Package.Open("SampleDoc.docx", FileMode.Open, FileAccess.Read))
{
PackageRelationship relationship =
wdPackage
.GetRelationshipsByType(documentRelationshipType)
.FirstOrDefault();
if (relationship != null)
{
Uri documentUri =
PackUriHelper.ResolvePartUri(
new Uri("/", UriKind.Relative),
relationship.TargetUri);
PackagePart documentPart = wdPackage.GetPart(documentUri);
// Get the officeDocument part from the package.
// Load the XML in the part into an XDocument instance.
XDocument xdoc =
XDocument.Load(XmlReader.Create(documentPart.GetStream()));
Console.WriteLine(xdoc.Root);
}
}
Imports <xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
Module Module1
Sub Main()
Dim documentRelationshipType = _
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Using wdPackage As Package = _
Package.Open("SampleDoc.docx", _
FileMode.Open, FileAccess.Read)
Dim docPackageRelationship As PackageRelationship = wdPackage _
.GetRelationshipsByType(documentRelationshipType).FirstOrDefault()
If (docPackageRelationship IsNot Nothing) Then
Dim documentUri As Uri = PackUriHelper.ResolvePartUri( _
New Uri("/", UriKind.Relative), _
docPackageRelationship.TargetUri)
Dim documentPart As PackagePart = wdPackage.GetPart(documentUri)
' Get the officeDocument part from the package.
' Load the XML in the part into an XDocument instance.
Dim xDoc As XDocument = _
XDocument.Load(XmlReader.Create(documentPart.GetStream()))
Console.WriteLine(xDoc.Root)
End If
End Using
End Sub
End Module