我有这样的OOXML文档的段落元素。 现在,我希望使用c#以编程方式从此文本中获取FootNoteRefrence id。
来自document.xml的文本
<w:p>
<w:r>
<w:rPr>
<w:rStyle w:val="FootnoteReference" />
</w:rPr>
<w:footnoteReference w:id="2" />
</w:r>
</w:p>
C#代码
private BodyPara writePara(BodyPara bPara2, OpenXmlElement pTag)
{
Footnotes fn = null;
foreach (var run in pTag.Descendants<Run>())
{
if (run.HasChildren)
{
foreach (var runProp in run.Descendants<RunProperties>())
{
foreach (var runStyle in runProp.Descendants<RunStyle>())
{
if (runStyle.Val != null)
{
string runSty = runStyle.Val.Value;
if (runSty == "FootnoteReference")
{
if (fn != null)
{
bPara2.FootNotes.Add(fn);
}
fn = new Footnotes();
}
else if (runSty == "CommentReference")
{
}
else
{
if (fn != null)
{
fn.FootText = fn.FootText + run.InnerText;
}
}
}
}
//FootnotesPart footnotesPart = wordDoc.MainDocumentPart.FootnotesPart;
//if (footnotesPart != null)
//{
// IEnumerable<Footnote> footnotes = footnotesPart.Footnotes.Elements<Footnote>();
// ...
//}
if (runProp.NextSibling() != null)
{
OpenXmlElement fr = runProp.NextSibling();
foreach (var fnref in fr)
{
if (fnref != null)
{
// fn.FootnoteID = fnref.Id.Value.ToString();
}
}
}
foreach (var shd in runProp.Descendants<Shading>())
{
if (shd.Fill != null)
{
string shdvalue = shd.Fill.Value;
}
}
}
}
}
return bPara2;
}
我正在使用它来获取每个脚注的脚注引用ID。 在这个循环中,我无法获得FootNoteReference类型的运行后代及其值。 请帮我解决这个问题。 谢谢。
答案 0 :(得分:0)
抱歉,我在参数中犯了一个错误,而不是在参数列表中使用Paragraph pTag
,我使用了OpenXmlElement pTag
。现在我把它从通用改为特定。它现在有效。