为什么我会收到一个OpenXmlUnknownElement?

4

这不算是一个问题,因为我已经找到了这个问题的原因。如下图所示,我正在尝试从OpenXml主体中检索后代元素:

Retrieving descendants from OpenXml body

使用以下代码检索后代元素:

using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace Testolini
{
    class Program
    {
        static void Main(string[] args)
        {
            var filename = Path.GetTempFileName();
            var word = WordprocessingDocument.Create(filename, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
            word.AddMainDocumentPart();
            word.MainDocumentPart.Document = new Document(
                                                new Body(
                                                    new Paragraph(
                                                        new Run(
                                                            new Paragraph(
                                                                new Run(
                                                                    new Text("test1"))),
                                                            new Paragraph(
                                                                new Run(
                                                                    new Text("test2"))),
                                                            new Paragraph(
                                                                new Run(
                                                                    new Text("test3")))))));
            word.Close();

            using (var memorystream = new MemoryStream(File.ReadAllBytes(filename)))
            {
                var word2 = WordprocessingDocument.Open(memorystream, true);

                var descendants = word2.MainDocumentPart.Document.Body.Descendants();

                word.Close();

            }
        }
    }
}

如果你遇到了相同的问题,可能是因为XML文件不符合ECMA标准。在我的情况下,问题是由于我嵌套了段落。当我使用bytearray和memorystream打开文档时,问题就出现了。看起来元素已经被验证,如果验证失败,它会变成OpenXmlUnknownElement。如果有更好的解释或者更精确的原因,我很乐意学习更多。

我认为这种情况发生在您更改文档以使其包含不应以此方式相关的父项下的子项时。这在您的解决方案中是可能的情况吗? - Maarten
是的。在我的情况下,您不能在运行(/段落)中有一个段落。我正在尝试找到一种方法来执行验证,以便这种情况不会发生。 - Stian Standahl
这可能会有所帮助:http://msdn.microsoft.com/zh-cn/library/office/bb497334.aspx - Maarten
2个回答

5
一个Run不能包含另一个Paragraph
以下是Run的有效子元素列表:

annotationRef(注释信息块)
br(断行)
commentReference(评论内容参考标记)
contentPart(内容部分)
continuationSeparator(连续分隔符标记)
cr(回车符)
dayLong(日期块-长日期格式)
dayShort(日期块-短日期格式)
delInstrText(已删除字段代码)
delText(已删除文本)
drawing(DrawingML对象)
endnoteRef(尾注参考标记)
endnoteReference(尾注参考)
fldChar(复杂字段字符)
footnoteRef(脚注参考标记)
footnoteReference(脚注参考)
instrText(字段代码)
lastRenderedPageBreak(最后计算的分页位置)
monthLong(日期块-长月份格式)
monthShort(日期块-短月份格式)
noBreakHyphen(不换行连字符字符)
object(嵌入式对象)
pgNum(页码块)
ptab(绝对位置制表符字符)
rPr(运行属性)
ruby(拼音指南)
separator(脚注/尾注分隔符标记)
softHyphen(可选连字符字符)
sym(符号字符)
t(文本)
tab(制表符字符)
yearLong(日期块-长年份格式)
yearShort(日期块-短年份格式)

以上内容来自MSDN
为什么需要“嵌套”段落?

我不知道。这个程序会自动生成OpenXML文件,但是在某个地方,段落嵌套导致了我的错误。 - Stian Standahl

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接