4

我将尝试非常清楚地解释这个问题。我在加载报告的地方使用 MicroSoftReportViewer。但在加载它之前我想改变一些东西。直到这里一切都好。我想使用 xpath,但是当我使用 XMLDocument 加载 rdlc( xml ) 文件时,xpath 表达式不起作用。唯一有效的 xpath 是 "\" 女巫获得根。我用记事本打开文件,发现第一个 xml 节点使用了这些模式

xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" 
xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"

我尝试使用添加了 XMLSchema 的 XMLReader 读取文件,但 xpath 仍然不起作用。请让我非常感谢代码的平静,看看如何加载文件以便 xpath 工作。

最好的问候, 约旦

4

1 回答 1

5

恐怕我们需要查看您的 XPath 语句才能确定,但​​我的猜测是名称空间的问题。

没有前缀的元素default namespace在上述文档的 which 中设置为
http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition

您的 XPath 查询现在需要在查询中包含这些名称空间。所以,一个 selectSingleNode( /elementnameicanseeinnotepad) 不会给你任何东西。

要在查询中添加命名空间,您必须使用XmlNamespaceManager类(或使用我不推荐的 XPath 的详细语法)。

// get an instance  
XmlNamespaceManager xMngr = new XmlNamespaceManager();
// associate the prefix ´def´ with the namespace-uri from the xml document we loaded
xMngr.AddNamespace( `def´, http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition);
// associate the prefix ´rd´ (same as used in document) with the namespace-uri from the xml document we loaded
xMngr.AddNamespace( `rd´, http://schemas.microsoft.com/SQLServer/reporting/reportdesigner);

// use the prefix(s) in the XPath query  
xDoc.DocumentElement.SelectSingleNode(´/def:elementnameiseeinnotepad´, xMngr );

希望这可以帮助。

于 2010-02-17T21:12:02.243 回答