Search code examples
c#asp.netdocx

How to read a .docx file in ASP.NET


When I read a .txt file it works and displays, but when I read a .docx file it reads but displays in XML format. How do I convert that XML format to .docx?

The code is:

protected void viewfile(object sender, EventArgs e)
{
          string path = (sender as LinkButton).CommandArgument;
          if (!string.IsNullOrEmpty(path))
          {
                  string[] readText = File.ReadAllLines(path);
                  StringBuilder strbuild = new StringBuilder();
                  foreach (string s in readText)
                  {
                          strbuild.Append(s);
                          strbuild.AppendLine();
                  }
                          TextBox1.Text = strbuild.ToString();
          }
}

Solution

  • Reference this , Using DocxToText to Extract Text from DOCX Files.

    You can extract text from .docx file like ,

    DocxToText dtt = new DocxToText(docxFileName);
    string text = dtt.ExtractText();