title: How to retrieve content based chapter and paragraph from the word file using open xml and c#4.0?
body: <p>I am using c# and open xml sdk 2.0 for accessing Word file.</p>
  <p>For that, now i want to retrieve a chapter and paragraph based on the given text.</p>
  <p>If the chapter contains my text then retrieve the paragraph containing that text...</p>
  <pre><code>FOR EXAMPLE: Given Word is: Chapter 1 - Events
  </code></pre>
  <p>Retrieve the chapter and paragraphs that containing the word <code>&quot;Chapter 1 - Events&quot;</code></p>
  <p>I want to search the given word on the Word file.</p>
  <p>If any matches found, then i want to display that methods and insert into database according with this schema</p>
  <p><a href="https://i.stack.imgur.com/41YQL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/41YQL.png" alt="enter image description here" /></a></p>
  <p>If matches not found, no need to get the paragraph.</p>
  <p>How i do?</p>
  <p><em>code-behind</em> (now the return on visual studio debug is empty)</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using DocumentFormat.OpenXml;
  using DocumentFormat.OpenXml.Packaging;
  using DocumentFormat.OpenXml.Wordprocessing;
  
  public partial class Default : Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              string file = @&quot;C:\Users\file.docx&quot;;
  
              using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(file, true))
              {
                  Body body = wordDoc.MainDocumentPart.Document.Body;
  
                  string paras = &quot;&quot;;
  
                  foreach (Paragraph p in wordDoc.MainDocumentPart.Document.Body.Descendants&lt;Paragraph&gt;().Where&lt;Paragraph&gt;(p =&gt; p.InnerText.Contains(&quot;Chapter [0-9]{1;} &quot;)))
                  {
                      paras += p.InnerText + &quot;&lt;br/&gt;&quot;;
                  }
  
                  Response.Write(paras);
              }
          }
      }
  }
  </code></pre>
  <p><em>file.docx</em></p>
  <pre><code>Chapter 1 - Events
   - alert or disservices
  Lorem ipsum dolor sit amet, consectetur adipiscing elit
  ….
  ….
  - significant activities
  Phasellus dui nunc, rutrum vitae dictum eleifend, ullamcorper hendrerit sem
  ….
  ….
  
  Chapter 2 – Safety
  - near miss
  Praesent venenatis convallis nunc, quis ultrices massa
  ….
  ….
  - security checks
  Phasellus mollis dapibus porta. Phasellus ac tristique dui
  ….
  ….
  
  Chapter 3 – Training
  - environment
  Nunc hendrerit scelerisque mauris vel eleifend
  ….
  ….
  - upkeep
  Mauris id elit nec nisl laoreet posuere. Donec ac molestie sem
  ….
  ….
  </code></pre>
  <p><em>database table</em></p>
  <pre><code>DROP TABLE IF EXISTS `chapters`;
  CREATE TABLE `chapters` (
    `chapter` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
    `subheading` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
    `contents` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
    `sID` int(11) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`sID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  </code></pre>