使用样式的 WordprocessingML 文档
较为复杂的 WordprocessingML 文档具有使用样式格式的段落。
对 WordprocessingML 文档的结构做一些注释是很有帮助的。WordprocessingML 文档存储在包中。包具有多个部分(“部分”在包上下文中具有明确的意义,部分实质上是压缩在一起组成包的多个文件)。如果一个文档包含使用样式格式的段落,将会有一个文档部分包含应用了样式的段落。还会有一个样式部分包含文档引用的样式。
访问包时,重要的是通过部分之间的关系来访问包,而不是使用任意路径来访问包。此问题超出了在 WordprocessingML 文档中使用内容教程的范围,但是本教程包含的示例程序演示了正确的方法。
使用样式的文档
WordprocessingML 文档的形状主题中提供的 WordML 示例是非常简单的文档。下面的文档则更为复杂:它具有使用样式格式的段落。查看构成 Office Open XML 文档的 XML 的最简单方法是运行输出 Office Open XML 文档部分的示例。
在下面的文档中,第一段具有 Heading1 样式。很多段落具有默认样式。还有一些段落具有 Code 样式。由于这种相对复杂性,这个文档更值得使用 LINQ to XML 来解析。
在那些使用非默认样式的段落中,段落元素具有名为 w:pPr 的子元素,该子元素又具有子元素 w:pStyle。该元素具有属性 w:val,该属性包含样式名称。如果段落具有默认样式,则意味着段落元素不具有 w:p.Pr 子元素。
<?xml version="1.0" encoding="utf-8"?>
<w:document
xmlns:ve="https://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="https://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="https://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp="https://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="https://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:wne="https://schemas.microsoft.com/office/word/2006/wordml">
<w:body>
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Heading1" />
</w:pPr>
<w:r>
<w:t>Parsing WordprocessingML with LINQ to XML</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0" />
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0">
<w:r>
<w:t>The following example prints to the console.</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0" />
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Code" />
</w:pPr>
<w:r>
<w:t>using System;</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Code" />
</w:pPr>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRPr="00876F34" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Code" />
</w:pPr>
<w:r w:rsidRPr="00876F34">
<w:t>class Program {</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRPr="00876F34" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Code" />
</w:pPr>
<w:r w:rsidRPr="00876F34">
<w:t xml:space="preserve"> public static void </w:t>
</w:r>
<w:smartTag w:uri="urn:schemas-microsoft-com:office:smarttags" w:element="place">
<w:r w:rsidRPr="00876F34">
<w:t>Main</w:t>
</w:r>
</w:smartTag>
<w:r w:rsidRPr="00876F34">
<w:t>(string[] args) {</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRPr="00876F34" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Code" />
</w:pPr>
<w:r w:rsidRPr="00876F34">
<w:t xml:space="preserve"> Console.WriteLine("Hello World");</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRPr="00876F34" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Code" />
</w:pPr>
<w:r w:rsidRPr="00876F34">
<w:t xml:space="preserve"> }</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRPr="00876F34" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Code" />
</w:pPr>
<w:r w:rsidRPr="00876F34">
<w:t>}</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0" />
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0">
<w:r>
<w:t>This example produces the following output:</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0" />
<w:p w:rsidR="00A75AE0" w:rsidRDefault="00A75AE0" w:rsidP="006027C7">
<w:pPr>
<w:pStyle w:val="Code" />
</w:pPr>
<w:r>
<w:t>Hello World</w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="00A75AE0" w:rsidSect="00A75AE0">
<w:pgSz w:w="12240" w:h="15840" />
<w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0" />
<w:cols w:space="720" />
<w:docGrid w:linePitch="360" />
</w:sectPr>
</w:body>
</w:document>