This is not really a question since I already have found the reason for this problem
这并不是一个真正的问题,因为我已经找到了这个问题的原因
The descendants are retrieved using this code.
使用此代码检索后代。
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();
}
}
}
}
If you get the same problem. It can be because the XML fil does not validate with the ECMA standard. In my case the problem was that i had nested paragraphs.
如果你遇到同样的问题。这可能是因为XML fil没有使用ECMA标准进行验证。在我的例子中,问题是我有嵌套的段落。
The problem shows up when I opened the document using bytearray and memorystream. It looks like the elements is validated and if the validation fails it becames an OpenXmlUnknownElement.
当我使用bytearray和memorystream打开文档时,问题就出现了。看起来元素是经过验证的,如果验证失败,它将成为一个OpenXmlUnknownElement。
If anyone have better explanation and maybe a more precise reason for this problem I would love to learn more about it.
如果有人对这个问题有更好的解释或者更准确的解释,我很乐意了解更多。
2 个解决方案
#1
2
A Run
cannot contain another Paragraph
.
运行不能包含另一段。
Here is a list of valid child elements for a 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)annotationRef(评论信息块)br(打破)commentReference(评论内容引用马克)contentPart(内容部分)continuationSeparator(延续分隔符标记)cr(回车)一整天(日期块——一整天格式)dayShort(块——短天日期格式)delInstrText(删除字段代码)delText(删除文本)图(DrawingML对象)endnoteRef(尾注引用马克)endnoteReference(尾注引用)fldChar(复数域字符)footnoteRef(脚注引用马克)footnoteReference(脚注引用)instrText(字段代码)lastRenderedPageBreak(最后计算分页符的位置)为期(块长月日期格式)monthShort(块——短月日期格式)noBreakHyphen(非打破连字符)对象(嵌入式对象)pgNum(页码块)ptab(制表符绝对位置)弹性分组环(属性)运行ruby(拼音指南)分离器(脚注/尾注分隔符标记)softHyphen(可选字符字符)对称(符号字符)t(文本)标签(标签字符)长(日期块-长年格式)短(日期块-短年格式)
Taken from MSDN.
从MSDN。
Why do you need "nested" paragraphs?
为什么需要“嵌套”段落?
#2
2
It seems the document is in an invalid state. I think this happens when you alter the document so that it contains children beneath parents that should not be related that way.
文件似乎处于无效状态。我认为,当你修改文件时,会发生这样的事情,因为它包含了父母下面的孩子,不应该这样做。
This might be of help: http://msdn.microsoft.com/en-us/library/office/bb497334.aspx
这可能有帮助:http://msdn.microsoft.com/en-us/library/office/bb497334.aspx
#1
2
A Run
cannot contain another Paragraph
.
运行不能包含另一段。
Here is a list of valid child elements for a 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)annotationRef(评论信息块)br(打破)commentReference(评论内容引用马克)contentPart(内容部分)continuationSeparator(延续分隔符标记)cr(回车)一整天(日期块——一整天格式)dayShort(块——短天日期格式)delInstrText(删除字段代码)delText(删除文本)图(DrawingML对象)endnoteRef(尾注引用马克)endnoteReference(尾注引用)fldChar(复数域字符)footnoteRef(脚注引用马克)footnoteReference(脚注引用)instrText(字段代码)lastRenderedPageBreak(最后计算分页符的位置)为期(块长月日期格式)monthShort(块——短月日期格式)noBreakHyphen(非打破连字符)对象(嵌入式对象)pgNum(页码块)ptab(制表符绝对位置)弹性分组环(属性)运行ruby(拼音指南)分离器(脚注/尾注分隔符标记)softHyphen(可选字符字符)对称(符号字符)t(文本)标签(标签字符)长(日期块-长年格式)短(日期块-短年格式)
Taken from MSDN.
从MSDN。
Why do you need "nested" paragraphs?
为什么需要“嵌套”段落?
#2
2
It seems the document is in an invalid state. I think this happens when you alter the document so that it contains children beneath parents that should not be related that way.
文件似乎处于无效状态。我认为,当你修改文件时,会发生这样的事情,因为它包含了父母下面的孩子,不应该这样做。
This might be of help: http://msdn.microsoft.com/en-us/library/office/bb497334.aspx
这可能有帮助:http://msdn.microsoft.com/en-us/library/office/bb497334.aspx