c# - 为什么我得到一个 OpenXmlUnknownElement?

标签 c# arrays 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。

如果有人对此问题有更好的解释,也许还有更准确的原因,我很乐意了解更多。

最佳答案

Run 不能包含另一个 Paragraph

这是Run 的有效子元素列表:

annotationRef (Comment Information Block)
br (Break)
commentReference (Comment Content Reference Mark)
contentPart (Content Part)
continuationSeparator (Continuation Separator Mark)
cr (Carriage Return)
dayLong (Date Block - Long Day Format)
dayShort (Date Block - Short Day Format)
delInstrText (Deleted Field Code)
delText (Deleted Text)
drawing (DrawingML Object)
endnoteRef (Endnote Reference Mark)
endnoteReference (Endnote Reference)
fldChar (Complex Field Character)
footnoteRef (Footnote Reference Mark)
footnoteReference (Footnote Reference)
instrText (Field Code)
lastRenderedPageBreak (Position of Last Calculated Page Break)
monthLong (Date Block - Long Month Format)
monthShort (Date Block - Short Month Format)
noBreakHyphen (Non Breaking Hyphen Character)
object (Embedded Object)
pgNum (Page Number Block)
ptab (Absolute Position Tab Character)
rPr (Run Properties)
ruby (Phonetic Guide)
separator (Footnote/Endnote Separator Mark)
softHyphen (Optional Hyphen Character)
sym (Symbol Character)
t (Text)
tab (Tab Character)
yearLong (Date Block - Long Year Format)
yearShort (Date Block - Short Year Format)

取自MSDN .

为什么需要“嵌套”段落?

关于c# - 为什么我得到一个 OpenXmlUnknownElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13684361/

相关文章:

c# - 代码中 "+= ( s, e )"是什么意思?

c# - Linq 的性能包含

c# - 字节序 java/.net

python - numpy 记录数组与时间?

PHP函数返回0,不知道为什么

c# - 将 outerXml 转换为 OpenXmlElement

c# - MessageBox.Show 会导致跨线程异常吗?

javascript - 将第一个元素移动到数组末尾的最快方法

openxml - 使用 C# 添加引用 OPEN XML sdk 中另一个工作簿中的单元格的公式

openxml - 如何在 Excel 工作表 OpenXML 2.0 C# 中创建图表/图形