Sti*_*ahl 4 c# bytearray openxml
这不是一个真正的问题,因为我已经找到了这个问题的原因
使用此代码检索后代。
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你遇到同样的问题。这可能是因为 XML 文件未通过 ECMA 标准验证。就我而言,问题是我有嵌套的段落。
当我使用 bytearray 和 memorystream 打开文档时,问题就出现了。看起来元素已经过验证,如果验证失败,它就会变成 OpenXmlUnknownElement。
如果有人对这个问题有更好的解释和更准确的原因,我很想了解更多。
ARun不能包含另一个Paragraph。
以下是 a 的有效子元素列表Run:
annotationRef (Comment Information Block)
br (Break)
commentReference (Comment Content Reference Mark)
contentPart (Content Part)
continuationSeparator (继续分隔符标记)
cr (回车)
dayLong (Date Block - Long Day Format)
dayShort (Date Block - Short Day Format) )
delInstrText (Deleted Field Code)
delText (Deleted Text)
绘图(DrawingML Object)
endnoteRef (Endnote Reference Mark)
endnoteReference (Endnote Reference)
fldChar (Complex Field Character)
footnoteRef (脚注参考标记)
footnoteReference (脚注参考)
instrText (Field Code)
lastRenderedPageBreak(上次计算分页符的位置)
monthLong(日期块 - 长月格式)
monthShort(日期块 - 短月格式)
noBreakHyphen(非中断连字符)
对象(嵌入对象)
pgNum(页码块)
ptab(绝对位置选项卡)字符)
rPr(运行属性)
ruby(语音指南)
分隔符(脚注/尾注分隔符标记)
softHyphen(可选连字符)
sym(符号字符)
t(文本)
制表符(制表符)
yearLong(日期块 - 长年格式)
yearShort (日期块 - 短年格式)
摘自MSDN。
为什么需要“嵌套”段落?