我有一个叫做Reader
的课public class Reader
这里是构造函数
public Reader(string fileName)
{
using (Package package = Package.Open(AppDomain.CurrentDomain.BaseDirectory + "\\" + fileName + ".docx"))
{
Document = new XmlDocument();
Document.Load(package.GetPart(new Uri("/word/document.xml", UriKind.Relative)).GetStream());
xmlNamespaceManager = new XmlNamespaceManager(Document.NameTable);
xmlNamespaceManager.AddNamespace("w", @"http://schemas.microsoft.com/office/word/2006/wordml");
}
}
还有一个名为ReadTextNodes的公共方法,我已经对其进行了测试。
public void ReadTextNodes()
{
var nodes = Document.SelectNodes("//w:t", xmlNamespaceManager);
Console.WriteLine(nodes.Count);
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.InnerText);
}
}
我使用的Xpath是“ // w:t”-我已将其链接到Word使用的XML命名空间“ w”(“ http://schemas.microsoft.com/office/word/2006/wordml”) 但是,此查询给了我零个节点。 当我用“ // *”替换时,控制台会很快用文本填充。 那么第一个查询怎么了?
答案 0 :(得分:0)
我发现我使用了错误的架构。 我将docx文件另存为XML文件,并在Visual Studio中打开,发现“ w”实际上已映射到“ http://schemas.openxmlformats.org/wordprocessingml/2006/main”