I am using c# and open xml sdk 2.0 for accessing Word file after upload on the server.
I want to retrieve a chapter and paragraph based on the given text.
The chapters are insert on the table chapter
The chapters and paragraphs are insert on the table chapter subheading
Using System.Linq
I can locate chapters Contains Chapter
foreach (Paragraph c in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(c => c.InnerText.Contains("Chapter")))
and subheading Starts With "- "
foreach (Paragraph p in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(p => p.InnerText.StartsWith("- ")))
But I have this blocking error when get Visual Studio debug
CS0103: The name 'c' does not exist in the current context
Please help me
My code below
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;
}
}
}
edit #1
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>(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, conn))
{
cmd.Parameters.AddWithValue("param1", 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, 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();
}
}
}
}
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;
}
}
}