2011-08-29 84 views
1

我正在使用altChunkId元素合併DOCX文檔文件,但無法查看控件XML元素的內容。無法合併來自Word 2010文件的XML文檔內容

我認爲這是因爲XML所採取內容的引用來自它被合併到的文件夾而不是文件夾本身。

在Word 2007中,該文件顯示內容控件的內容,而在Word 2010中則爲空白。

重現步驟:

  1. 打開Word 2010
  2. 轉到開發人員選項卡。
  3. 添加「純文本內容控制」。
  4. 另存爲a.docx
  5. 創建XML與Word 2007 content control toolkit
  6. 複製a.docx綁定到一個新的文件b.docx
  7. 代碼合併:

    using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.IO.Packaging; 
    using System.Linq; 
    using System.Text; 
    using System.Xml; 
    using DocumentFormat.OpenXml; 
    using DocumentFormat.OpenXml.Packaging; 
    using DocumentFormat.OpenXml.Spreadsheet; 
    using DocumentFormat.OpenXml.Wordprocessing; 
    
    class Class1 
    { 
        static void Main(string[] args) 
        { 
         string doc2 = @"b.docx"; 
         string doc1 = @"a.docx"; 
         using (var myDoc = WordprocessingDocument.Open(doc2, true)) 
         { 
          string altChunkId = "AltChunkId1"; 
          var mainPart2 = myDoc.MainDocumentPart; 
    
          var chunk = mainPart2.AddAlternativeFormatImportPart(
          AlternativeFormatImportPartType.WordprocessingML, altChunkId); 
          using (var fileStream = File.Open(doc1, FileMode.Open)) 
          { 
           chunk.FeedData(fileStream); 
           AltChunk altChunk = new AltChunk(); 
           altChunk.Id = altChunkId; 
           mainPart2.Document.Body.InsertAfter(altChunk, mainPart2.Document.Body.Elements<Paragraph>().Last()); 
           mainPart2.Document.Save(); 
          } 
         } 
        } 
    } 
    
  8. 與Word 2010打開b.docx,你會看到純文本內容的控制是空的。

  9. 使用Word 2007打開b.docx,您將看到純文本內容控件不爲空。
+0

您可以發佈b.docx的地方,我們可以看一下嗎? – JasonPlutext

+0

你可以分享代碼和文檔嗎? MS已經刪除了自定義XML支持,但是因爲您使用的數據綁定內容控件不應該是問題。 –

回答