【发布时间】:2015-04-26 04:31:23
【问题描述】:
我正在编写一个 .NET 应用程序,它应该读取一个 200 页左右的 .docx 文件(通过 DocumentFormat.OpenXML 2.5),以查找该文档应包含的某些标签的所有出现。 需要明确的是,我不是在寻找 OpenXML 标记,而是在寻找应该由文档编写者设置到文档中的标记,作为我需要在第二阶段填写的值的占位符。 此类标签应采用以下格式:
<!TAG!>
(其中 TAG 可以是任意字符序列)。 正如我所说,我必须找到所有此类标签的出现以及(如果可能)定位找到标签出现的“页面”。 我在网上发现了一些东西,但不止一次,基本方法是将文件的所有内容转储到一个字符串中,然后不管 .docx 编码如何查看该字符串。这要么导致误报,要么根本不匹配(而测试 .docx 文件包含多个标签),其他示例可能有点超出我对 OpenXML 的了解。 查找此类标签的正则表达式模式应该是这样的:
<!(.)*?!>
标签可以在整个文档中找到(在表格、文本、段落,以及页眉和页脚内)。
我正在使用 Visual Studio 2013 .NET 4.5 进行编码,但如果需要,我可以返回。 附言我更喜欢不使用 Office Interop API 的代码,因为目标平台不会运行 Office。
我可以生成的最小的 .docx 示例将其存储在文档中
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="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:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" 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:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>
<w:p w:rsidR="00CA7780" w:rsidRDefault="00815E5D">
<w:pPr>
<w:rPr>
<w:lang w:val="en-GB"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:lang w:val="en-GB"/>
</w:rPr>
<w:t>TRY</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00815E5D" w:rsidRDefault="00815E5D">
<w:pPr>
<w:rPr>
<w:lang w:val="en-GB"/>
</w:rPr>
</w:pPr>
<w:proofErr w:type="gramStart"/>
<w:r>
<w:rPr>
<w:lang w:val="en-GB"/>
</w:rPr>
<w:t><!TAG1</w:t>
</w:r>
<w:proofErr w:type="gramEnd"/>
<w:r>
<w:rPr>
<w:lang w:val="en-GB"/>
</w:rPr>
<w:t>!></w:t>
</w:r>
</w:p>
<w:p w:rsidR="00815E5D" w:rsidRPr="00815E5D" w:rsidRDefault="00815E5D">
<w:pPr>
<w:rPr>
<w:lang w:val="en-GB"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:lang w:val="en-GB"/>
</w:rPr>
<w:t>TRY2</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:sectPr w:rsidR="00815E5D" w:rsidRPr="00815E5D">
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1417" w:right="1134" w:bottom="1134" w:left="1134" w:header="708" w:footer="708" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
最好的问候, 迈克
【问题讨论】:
-
所以您实际上已经提取了 xml 文档?
-
如果可以,您能给我们看一个小样本吗?
-
@Florian “提取”是什么意思?无论如何,如果我找到方法,我可以在此处附加文件....
-
docx 文件实际上是一个 .zip 文件...包含一个文件夹结构,其中包含一些 xml 文档...您可以尝试将 .docx 重命名为 .zip(必须禁用省略资源管理器中的已知文件扩展名)
-
是的,我知道,但我认为 DocumentFormat.OpenXML 确实会自行提取文件?我错了吗?