2015-09-29 62 views
1

我正在使用Open XML SDK 2.5在我的控制檯應用程序中讀取.docx文件。DOCX XML不象Word那樣代表換行符?

在使用Open XML SDK打開Word時,Word顯示文檔的方式以及如何用XML表示文檔似乎存在一些差異。

這是我舉例來說,如空格可見在Word中看到:


enter image description here


所以在我的應用我有這一段爲DocumentFormat.OpenXml.Wordprocessing.Paragraph對象的引用。在瀏覽Open XML文檔後,我很清楚XML格式中沒有「行」的表示。所以我能做的最好的是我的Paragraph,最接近一條線的是Run對象。在此示例中,Paragraph節點具有6個Run對象的集合。如果我得到ParagraphInnerXml屬性在這個例子這裏是它的外觀:

<w:pPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:pStyle w:val=\"PlainText\" /><w:numPr><w:ilvl w:val=\"0\" /><w:numId w:val=\"17\" /></w:numPr><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr></w:pPr><w:r w:rsidRPr=\"000558F8\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t>Should we use the term 「Verify」 instead of 「Confirm」</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00F5335C\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t xml:space=\"preserve\"> as per work instruction</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00411638\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t>?</w:t></w:r><w:r w:rsidR=\"000558F8\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:br /><w:t>Med</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"003E76BD\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr><w:br /><w:t xml:space=\"preserve\">JD: </w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00A118AB\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr><w:t>Done.</w:t></w:r> 

所有我看到的是段落屬性節點和6個節點運行。正如你所看到的運行節點不等同於線。從Word中查看我的示例,我發現該段落有2個回車符,我希望這將由3個「行」表示。然而,在XML中,我得到了6次運行,這似乎與3條線近似,但由於某些原因,某些線路似乎是任意分割的。

真正的問題是,我沒有看到任何方式來解釋運行節點的方式,我可以重建我在Word中的示例中的行結構。例如,沒有任何跡象表明運行1,2和3一起構成第1行。

我需要解析300多個依賴於換行格式的文檔。我需要換行符,我怎樣才能得到它們? Open XML SDK可以實現嗎?

在此先感謝。

回答

1

您在XML中查找的元素是Break元素,它是<w:br />

從文檔,這個XML:

<w:r> 
    <w:t>This is</w:t> 
    <w:br/> 
    <w:t xml:space="preserve"> a simple sentence.</w:t> 
</w:r> 

會產生

這是
一個簡單的句子。

我已經對您的XML進行了修飾,並在此答案的末尾標記了Breaks

Runs不用於確定行,而是它們是包含具有相同屬性的文本的邏輯塊。例如,假設我有以下文字:

測試荷蘭國際集團

注意,ing爲黑體。在OpenXML中,這需要兩次運行,一次爲test,另一次爲ing,因爲它們具有不同的屬性。 XML配置將是這樣的:

<w:r> 
    <w:t>Test</w:t> 
</w:r> 
<w:r w:rsidRPr="004750BC"> 
    <w:rPr> 
     <w:b /> 
    </w:rPr> 
    <w:t>ing</w:t> 
</w:r> 

<w:rPr><w:b />表示大膽的運行性能。

您與斷裂XML強調:

<w:pPr 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:pStyle w:val="PlainText" /> 
    <w:numPr> 
     <w:ilvl w:val="0" /> 
     <w:numId w:val="17" /> 
    </w:numPr> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" /> 
     <w:b /> 
    </w:rPr> 
</w:pPr> 
<w:r w:rsidRPr="000558F8" 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" /> 
    </w:rPr> 
    <w:t>Should we use the term 「Verify」 instead of 「Confirm」</w:t> 
</w:r> 
<w:r w:rsidRPr="000558F8" w:rsidR="00F5335C" 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" /> 
    </w:rPr> 
    <w:t xml:space="preserve"> as per work instruction</w:t> 
</w:r> 
<w:r w:rsidRPr="000558F8" w:rsidR="00411638" 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" /> 
    </w:rPr> 
    <w:t>?</w:t> 
</w:r> 
<w:r w:rsidR="000558F8" 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" /> 
    </w:rPr> 
    <w:br /> <!-- break here --> 
    <w:t>Med</w:t> 
</w:r> 
<w:r w:rsidRPr="000558F8" w:rsidR="003E76BD" 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" /> 
     <w:b /> 
    </w:rPr> 
    <w:br /> <!-- break here --> 
    <w:t xml:space="preserve">JD: </w:t> 
</w:r> 
<w:r w:rsidRPr="000558F8" w:rsidR="00A118AB" 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" /> 
     <w:b /> 
    </w:rPr> 
    <w:t>Done.</w:t> 
</w:r> 
+0

完美!這正是我需要的。 對於任何想做同樣事情的人來說,在我的實現中,我只是將所有'Paragraph'的'Run'子元素和它們的'InnerText'屬性添加到單個字符串中,並在'Run '對象包含一個'Break'類型的子對象。 –