Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#.NET正在检索特定注释引用的文本的列表号_C#_.net_Ms Word_Openxml_Docx - Fatal编程技术网

C#.NET正在检索特定注释引用的文本的列表号

C#.NET正在检索特定注释引用的文本的列表号,c#,.net,ms-word,openxml,docx,C#,.net,Ms Word,Openxml,Docx,我试图检索由特定注释引用的一段文本的列表号,如这里所示的示例所示。在本例中,应检索3.3.1.1.1.-I。这是一种自定义列表样式。 我该怎么做呢 已经使用OpenXMLSDK完成了以下工作 public static void GetCommentsFromDocument(string fileName) { int pageCount = 0; Dictionary<int, string> sectionDi

我试图检索由特定注释引用的一段文本的列表号,如这里所示的示例所示。在本例中,应检索3.3.1.1.1.-I。这是一种自定义列表样式。

我该怎么做呢

已经使用OpenXMLSDK完成了以下工作

public static void GetCommentsFromDocument(string fileName)
        {

            int pageCount = 0;
            Dictionary<int, string> sectionDict = new Dictionary<int, string>(); // ID linking section and comment, section text
            Dictionary<int, string> commentDict = new Dictionary<int, string>(); // ID linking section and comment, comment text

            using (var wordDoc = WordprocessingDocument.Open(fileName, false))
            {

                MainDocumentPart mainPart = wordDoc.MainDocumentPart;
                var document = mainPart.Document;
                var commentsPart = mainPart.WordprocessingCommentsPart;


                //var comments = commentsPart.Comments;


                //foreach (Comment comment in comments)
                foreach (Comment comment in commentsPart.Comments.Elements<Comment>())
                {
                    string commentId = comment.Id;
                    string commentText = comment.InnerText;

                    // Console.WriteLine("ID: {0}  | Txt: {1}", commentId, commentText);

                    OpenXmlElement rangeStart = document.Descendants<CommentRangeStart>().Where(c => c.Id == commentId).FirstOrDefault();
                    List<OpenXmlElement> referenced = new List<OpenXmlElement>();

                    if (rangeStart==null)
                    {
                        continue;
                    }

                    // rangeStart = rangeStart.NextSibling(); //Whats the point of this?

                    while (!(rangeStart is CommentRangeEnd))
                    {
                        // Console.WriteLine("C_ID: {0} | Comment: {1} | SectionText: {2}", commentId, commentText, rangeStart.InnerText);
                        //section.Add();
                        if (!commentDict.ContainsKey(Convert.ToInt32(commentId)))
                        {
                            commentDict.Add(Convert.ToInt32(commentId), commentText);
                            sectionDict.Add(Convert.ToInt32(commentId), rangeStart.InnerText);
                        }
                        else
                        {
                            commentDict[Convert.ToInt32(commentId)] += commentText;
                            sectionDict[Convert.ToInt32(commentId)] += rangeStart.InnerText;
                        }



                        rangeStart = rangeStart.NextSibling();
                    }


                    foreach(var com in commentDict)
                    {
                        int comId = com.Key;
                        string commText = com.Value;
                        string sectionText = sectionDict[comId];
                        Console.WriteLine("C_ID: {0} | Comment: {1} | SectionText: {2}", comId, commText, sectionText);
                    }

                }
                Console.WriteLine("finished");

                Console.ReadKey();
            }
        }
编辑:使用OpenXML power tools库中的RetrieveListItem方法是否可行?它采用以下参数:

RetrieveListItem(WordprocessingDocument wordDoc, XElement paragraph)
简短的回答是“是”

下面是一个单元测试,它演示了如何使用
ListItemRetriever.RetrieveListItem()
方法

使用System.Linq;
使用System.Xml.Linq;
使用DocumentFormat.OpenXml.Packaging;
使用OpenXmlPowerTools;
使用Xunit;
使用Xunit.抽象;
命名空间CodeSnippets.Tests.OpenXml.Wordprocessing
{
公共类ListItemRetrieverTests
{
私有只读ITestOutputHelper\u输出;
公共ListItemRetrieverTests(ITestOutputHelper输出)
{
_输出=输出;
}
[事实]
public void RetrieveListItem\u DocumentWithNumberedLists\u ListItemSuccessfullyRetrieved()
{
const string path=“资源\\编号列表.docx”;
使用WordprocessingDocument wordDoc=WordprocessingDocument.Open(路径,false);
XElement document=wordDoc.MainDocumentPart.GetXElement();
foreach(文件中的删除段落。后代(W.p))
{
string listItem=ListItemRetriever.RetrieveListItem(wordDoc,段落);
string text=段落.子体(W.t).Select(t=>t.Value).StringConcatenate();
_output.WriteLine(string.IsNullOrEmpty(listItem)?text:$“{listItem}{text}”);
}
}
}
}
假设您正在以下
w:document
元素上使用它:


标题
第一正文
第一个字母
第二个字母
第二正文
第一位小数
第二位小数
第一品目1
第一品目2
第一机构
第二标题2
第二机构
第二品目1
第三品目2
第一品目3
第二标题3
第一品目4
第三体
第二标题4
第四机构
测试输出如下所示:

Title
First Body Text
(a) First letter
(b) Second letter
Second Body Text
(1) First decimal
(2) Second decimal
1 First Heading 1
1.1 First Heading 2
First Body
1.2 Second Heading 2
Second Body
2 Second Heading 1
2.1 Third Heading 2
2.1.1 First Heading 3
2.1.2 Second Heading 3
2.1.2.1 First Heading 4
Third Body
2.1.2.2 Second Heading 4
Fourth Body

源代码和测试文档可以在我的GitHub存储库中找到。

感谢您接受我的回答。如果有帮助的话,也请投票。
Title
First Body Text
(a) First letter
(b) Second letter
Second Body Text
(1) First decimal
(2) Second decimal
1 First Heading 1
1.1 First Heading 2
First Body
1.2 Second Heading 2
Second Body
2 Second Heading 1
2.1 Third Heading 2
2.1.1 First Heading 3
2.1.2 Second Heading 3
2.1.2.1 First Heading 4
Third Body
2.1.2.2 Second Heading 4
Fourth Body