使用OpenXml-sdk和C#来访问Word文件的当前上下文中不存在名称“ c”

问题描述

我正在使用c#并在服务器上上传后打开xml sdk 2.0来访问Word文件

我想根据给定的文本检索章节和段落。

这些章插入在表chapter

各章和各段插入在表章subheading

使用System.Linq可以找到章节Contains Chapter

foreach (Paragraph c in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(c => c.InnerText.Contains("Chapter")))

和副标题Starts With "- "

foreach (Paragraph p in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(p => p.InnerText.StartsWith("- ")))

但是在进行Visual Studio调试时出现此阻止错误

CS0103: The name 'c' does not exist in the current context

请帮助我

我的下面的代码

protected void UploadButton_Click(object sender,EventArgs e)
{
    if (FileUploadControl.HasFile && FileUploadControl.PostedFile.ContentLength > 0)
    {
        try
        {
            var allowedExtensions = new string[] { "docx" };
            var extension = Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower().Replace(".","");

            if (allowedExtensions.Contains(extension))
            {
                string filename = Path.GetFileName(FileUploadControl.FileName);

                FileUploadControl.SaveAs(Server.MapPath("public/") + filename);
                StatusLabel.Text = "Upload status: File uploaded!";

                file = Server.MapPath("public/") + filename;

                using (WordprocessingDocument wordDoc =
                    WordprocessingDocument.Open(file,true))
                {
                    body = wordDoc.MainDocumentPart.Document.Body;
                    paras = "";

                    foreach (Paragraph c in
                        wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(c => c.InnerText.Contains("Chapter")))
                    {
                        paras += c.InnerText + "<br/>";

                        strsql = @"INSERT IGnorE INTO Chapters (chapter) VALUES (?);";

                        using (MysqLConnection conn =
                            new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                        {
                            conn.open();
                            using (MysqLCommand cmd =
                                new MysqLCommand(strsql,conn))
                            {
                                cmd.Parameters.AddWithValue("param1",c.InnerText);
                                cmd.ExecuteNonQuery();
                            }
                            conn.Close();
                        }
                    }


                    foreach (Paragraph p in
                            wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(p => p.InnerText.StartsWith("- ")))
                    {
                        foreach (Run r in p.Descendants<Run>())
                        {
                            RunProperties rProp = r.RunProperties;

                            if (rProp.Bold != null)
                            {
                                paras += p.InnerText + "<br/>";

                                strsql = @"INSERT IGnorE INTO subheading (subheading,chapter) VALUES (?,?);";

                                using (MysqLConnection conn =
                                    new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                                {
                                    conn.open();
                                    using (MysqLCommand cmd =
                                        new MysqLCommand(strsql,conn))
                                    {
                                        cmd.Parameters.AddWithValue("param1",p.InnerText);
                                        cmd.Parameters.AddWithValue("param2",c.InnerText);
                                        cmd.ExecuteNonQuery();
                                    }
                                    conn.Close();
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
            }
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file Could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

修改#1

protected void UploadButton_Click(object sender,true))
                {
                    body = wordDoc.MainDocumentPart.Document.Body;
                    paras = "";

                    foreach (Paragraph c in
                        wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(somethingElse => somethingElse.InnerText.Contains("Sezione")))
                    {
                        paras += c.InnerText + "<br/>";

                        strsql = @"INSERT IGnorE INTO Chapters (chapter) VALUES (?);";

                        using (MysqLConnection conn =
                            new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                        {
                            conn.open();
                            using (MysqLCommand cmd =
                                new MysqLCommand(strsql,c.InnerText);
                                cmd.ExecuteNonQuery();
                            }
                            conn.Close();
                        }



                        foreach (Paragraph p in
                                wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(somethingElse => somethingElse.InnerText.StartsWith("- ")))
                        {
                            foreach (Run r in p.Descendants<Run>())
                            {
                                RunProperties rProp = r.RunProperties;

                                if (rProp.Bold != null)
                                {
                                    paras += p.InnerText + "<br/>";

                                    strsql = @"INSERT IGnorE INTO subheading (subheading,?);"; //

                                    using (MysqLConnection conn =
                                        new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                                    {
                                        conn.open();
                                        using (MysqLCommand cmd =
                                            new MysqLCommand(strsql,conn))
                                        {
                                            cmd.Parameters.AddWithValue("param1",p.InnerText);
                                            cmd.Parameters.AddWithValue("param2",c.InnerText);
                                            cmd.ExecuteNonQuery();
                                        }
                                        conn.Close();
                                    }
                                }
                            }
                        }
                        Response.Write(paras);
                    }
                    Response.Write(paras);
                }
            }
            else
            {
                StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
            }
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file Could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

解决方法

尝试

base::aggregate()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...