OpenXml表错误“< p>"每个</tc"元素之前都需要元素 [英] OpenXml Table error "<p> elements are required before every </tc>"

查看:139
本文介绍了OpenXml表错误“< p>"每个</tc"元素之前都需要元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Word模板,然后通过OpenXML SDK处理该模板,以用数据库查询中的数据替换文档的某些内容.

I have created a Word template that I am then processing via the OpenXML SDK to replace some of the content of the document with data from a database query.

该模板由一些基本文本组成,这些文本在我要替换文本的位置注入了纯文本内容"控件.然后,我将这些控件中的文本用作查找替换值的关键.在大多数情况下,这可以正常工作(我只是更新了Text对象的Text属性).

The template consists of some basic text with Plain Text Content controls injected in the places that I want to replace the text. I am then using the text in these controls as a key to lookup the replacement values. In most cases, this is working fine (I simply update the Text property of the Text object).

在一种情况下,我用表替换了文本.在这种情况下,我用代码建立了一个表,然后用新的Table对象替换了SdtContentRun对象(Run对象的父对象,而Run对象又是Text对象的父对象)的内容...

In one case I am replacing the text with a table. In this case, I build up a table in code and then I replace the content of the SdtContentRun object (the parent of the Run object, which in turn is the parent of the Text object) with the new Table object...

var sdtContentRunElements =
  from sdtContentRun in this.Document.MainDocumentPart.RootElement.Descendants<SdtContentRun>()
  select sdtContentRun;

sdtContentRunElements.ForEach(sdtContentRunElement => {

  Run firstRunElement = sdtContentRunElement.Descendants<Run>().FirstOrDefault();
  if (firstRunElement != null) {

    Text firstTextElement = firstRunElement.Descendants<Text>().FirstOrDefault();
    if (firstTextElement != null) {

      switch (firstTextElement.Text) {

        case TableBookmark:

          Table advisoryTable = new Table(...); // See below
          OpenXmlElement parent = firstRunElement.Parent;
          parent.RemoveAllChildren();
          parent.Append(advisoryTable);
          break;

        case ContractorItemAdvisoriesLetter.ContractorCodeBookmark:

          firstTextElement.Text = @"New text";
          break;

      }
    }
  }
}

}

这将导致以下XML(摘自Microsoft Office的Open XML SDK 2.0生产率工具)...

This results in the following XML (taken from the Open XML SDK 2.0 Productivity Tool for Microsoft Office)...

<w:sdtContent xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:tbl>
    <w:tr>
      <w:tc>
        <w:p>
          <w:r>
            <w:t>Lorem ipsum dolor sit amet</w:t>
          </w:r>
        </w:p>
      </w:tc>
    </w:tr>
  </w:tbl>
</w:sdtContent>

(表的某些内容已被删除,因为我认为这只会使问题蒙上阴影)

(Some of the content of the table has been removed as I felt it would simply cloud the issue)

当我尝试在Word中打开文档时,出现错误.正在报告的错误是...

When I then try to open the document in Word, I get an error. The error being reported is...

遇到错误的单元格映射.可能缺少的段落 元素. < p>每个</tc>

Ambuguous cell mapping encountered. Possible missing paragraph element. <p> elements are required before every </tc>

可能值得一提的是,当我在Microsoft Office的Open XML SDK 2.0生产力工具中查看文档资源管理器时,w:tbl元素(及其中包含的所有元素)被识别为OpenXmlUnknownElement,而不是被识别为Table对象.我不知道这是否相关,或者它是否是SDK工具的古怪之处.

It is probably worth mentioning that when I view the document explorer in Open XML SDK 2.0 Productivity Tool for Microsoft Office, the w:tbl element (and all elements contained within) are being recognised as OpenXmlUnknownElement rather than being recognised as Table objects. I do not know if this is relevant or whether it is a quirk of the SDK tool.

很明显,我缺少了一些东西.据我从表类定义,将aw:tbl放在aw:sdtContent内是完全合法的(除非我没有正确阅读),所以我现在对可能的问题不知所措.如果您用Google搜索表"和"OpenXmlUnknownElement",结果也很少,这些结果似乎都与我的问题无关.

Clearly I am missing something. As far as I can tell from the Table class definition, it is perfectly legal to place a w:tbl inside a w:sdtContent (unless I am reading this incorrectly) so I am now at a loss as to what the problem may be. There are also very few results if you google "Table" and "OpenXmlUnknownElement" with none of the results appearing to be related to my problem.

有什么建议吗?

进一步的调查似乎表明问题实际上出在另一个表的嵌入内部,该表被添加到上面添加的表的一个单元格中.新的Table对象被直接添加到TableCell对象,根据上面引用的Table的文档,再次应该可以接受...

Further investigation seems to indicate that the problem actually lies in the embedding of another table within one of the cells of the table being added above. The new Table object is being added directly to a TableCell object which again, according to the documentation for Table referenced above, should be acceptable...

Table advisoryTable = new Table();

advisories.ForEach(advisory => {

    advisoryTable.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text(advisory.NoteText))))));

    advisory.ItemAdvisories.ForEach(itemAdvisory => {

        Item item = itemAdvisory.Item;

        Table itemTable = new Table();
        itemTable.Append(new TableRow[] {

            new TableRow(new TableCell[] {
                new TableCell(new Paragraph(new Run(new Text(string.Format(@"Item {0}", item.Sequence)))))
            })

        });

        advisoryTable.Append(new TableRow(new TableCell(itemTable)));

    });

});

导致...

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:tr>
    <w:tc>
      <w:p>
        <w:r>
          <w:t>Lorem ipsum dolor sit amet</w:t>
        </w:r>
      </w:p>
    </w:tc>
  </w:tr>
  <w:tr>
    <w:tc>
      <w:tbl>
        <w:tr>
          <w:tc>
            <w:p>
              <w:r>
                <w:t>Item 1</w:t>
              </w:r>
            </w:p>
          </w:tc>
        </w:tr>
      </w:tbl>
    </w:tc>
  </w:tr>
</w:tbl>

我尝试在TableCell和嵌入的Table之间添加一个段落,但这只会导致一个新的错误,这次是未指定的错误".

I have tried adding a Paragraph between the TableCell and the embedded Table, but this simply results in a new error, this time "unspecified error".

推荐答案

已排序. 原来的错误消息真的无法更清晰地显示出来!

Sorted. The original error message could not really have been any clearer!

表单元格的最后一个子级必须是一个段落.在嵌入式表解决问题之后,只需添加一个空段落即可.

The last child of a table cell must be a paragraph. Simply adding an empty paragraph after the embedded table resolved the problem...

advisoryTable.Append(new TableRow(new TableCell(itemTable, new Paragraph())));

我最终通过在Word-2010中创建一个由表中的表组成的文档,然后在与上述Microsoft Office工具相同的Open XML SDK 2.0生产率工具中查看生成的XML,发现了这一点.

I eventually discovered this by creating a document in Word-2010 that consisted of a table within a table and then looking at the resulting XML in the same Open XML SDK 2.0 Productivity Tool for Microsoft Office tool used above.

这篇关于OpenXml表错误“&lt; p&gt;"每个&lt;/tc&quot;元素之前都需要元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆