0

我有一个像这样的 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 类型的 Run 的后代及其值。 请帮我解决这个问题。谢谢你。

4

1 回答 1

0

对不起,我在参数中做错了,我没有Paragraph pTag在参数列表中使用,而是使用OpenXmlElement pTag. 现在我将它从通用更改为特定。它现在有效。

于 2013-07-12T06:36:22.690 回答