代码之家  ›  专栏  ›  技术社区  ›  IordanTanev

使用xpath和rdlc报告

  •  4
  • IordanTanev  · 技术社区  · 16 年前

    我会尽量解释清楚这个问题。我在加载报表的位置使用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。

    最好的问候, 约旦

    1 回复  |  直到 14 年前
        1
  •  5
  •   Chepene winstead    14 年前

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

    未加前缀的元素位于 default namespace 上面的文档将其设置为
    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 );
    

    希望这有帮助。