我在下面的代码示例中尝试使用Ancestor属性来查找表节点中的段落节点,但它并不适用于所有情况。
using (WordprocessingDocument Docx = WordprocessingDocument.Open(DocxStream, true))
{
Body DocxBody = Docx.MainDocumentPart.Document.Body;
List<OpenXmlElement> EachPara = new List<OpenXmlElement>();//Paragraph element
foreach (OpenXmlElement bodyChild in DocxBody.ChildElements)
{
if (bodyChild is Paragraph)
{
if (bodyChild.Ancestors<Table>().ToList().Count > 0)
{
}
}
}
// List<OpenXmlElement> EachPara = DocxBody.ChildElements.ToList().Where(eachChild => eachChild is Paragraph).ToList();
foreach (OpenXmlElement lstPara in EachPara)
{
if (lstPara.ChildElements.Any(ch => ch.LocalName.ToLower().Equals("run")))
{
}
else
{
}
}
}