Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath 如何在C#中从word(docx)文档中获取文本?_Xpath_Docx_Openxml_Wordprocessingml - Fatal编程技术网

Xpath 如何在C#中从word(docx)文档中获取文本?

Xpath 如何在C#中从word(docx)文档中获取文本?,xpath,docx,openxml,wordprocessingml,Xpath,Docx,Openxml,Wordprocessingml,我试图从word文档中获取纯文本。具体来说,xpath给我带来了麻烦。如何选择标签?这是我的密码 public static string TextDump(Package package) { StringBuilder builder = new StringBuilder(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(package.GetPart(new Uri("/word/document.xm

我试图从word文档中获取纯文本。具体来说,xpath给我带来了麻烦。如何选择标签?这是我的密码

public static string TextDump(Package package)
{
    StringBuilder builder = new StringBuilder();

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(package.GetPart(new Uri("/word/document.xml", UriKind.Relative)).GetStream());

    foreach (XmlNode node in xmlDoc.SelectNodes("/descendant::w:t"))
    {
        builder.AppendLine(node.InnerText);
    }
    return builder.ToString();
}
请看下面的图片。这里有一些关于如何处理文档的示例


虽然我没有使用它,但是您也可以查看它。

您的问题是XML名称空间
SelectNodes
不知道如何将
转换为完整的命名空间。因此,您需要使用重载,它将
XmlNamespaceManager
作为第二个参数。我对您的代码进行了一些修改,它似乎起到了作用:

    public static string TextDump(Package package)
    {
        StringBuilder builder = new StringBuilder();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(package.GetPart(new Uri("/word/document.xml", UriKind.Relative)).GetStream());
        XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);
        mgr.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

        foreach (XmlNode node in xmlDoc.SelectNodes("/descendant::w:t", mgr))
        {
            builder.AppendLine(node.InnerText);
        }
        return builder.ToString();
    }