I'm trying to connect to a Microsoft word document (.docx) to read values from tables located in the .docx. I'm using Open-XML SDK 2.0 to make the connection to the .docx file. So far after looking for examples and ideas, I have this,
public static string TextFromWord(string file)
{
const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
StringBuilder textBuilder = new StringBuilder();
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(filename,false))
{
//Manage namespaces to perform Xpath queries
NameTable nt = new NameTable();
XmlNamespaceManager nsManger = new XmlNamespaceManger(nt);
nsManager.AddNamespace("w", wordmlNamespace);
//Get the document part from the package.
//Load the XML in the document part into an XmlDocument instance.
XmlDocument xdoc = new XmlDocument(nt);
xdoc.Load(wdDoc.MainDocumentPart.GetStream());
XmlNodeList paragraphNodes = xdoc.SelectNodes("//w:p", nsManager);
foreach (XmlNode paragraphNode in paragraphNodes)
{
XmlNodeList textNodes = paragraphNode.SelectNodes(".//w:t", nsmanager);
foreach (System.Xml.XmlNode textNode in textNodes)
{
textBuilder.Append(textNode.InnerText);
}
textBuilder.Append(Environment.NewLine);
}
}
return textBuilder.ToString();
}
The code works when there is just text in the .docx but fails when the text is in tables. Is there a way to fix this so it can work with tables in a .docx?