TableRows 元素上的 RDLC SelectNodes
我正在努力返回 RDLC 文件的 TableRows 元素下的行元素的 XMLNodeList。有很多命名空间是原因的例子,但我尝试了所有这些,但无法从我的 XPath 查询中获取任何节点,尽管公平地说,我并不真正理解默认/命名空间等。我已经检查了 Quickwatch 中的默认命名空间属性,它说它是 ”” ??虽然我不知道为什么或如何将其设置为只读。
我用以下方式定义了我的命名空间:
Dim doc As XmlDocument = New XmlDocument()
doc.Load(fname)
Dim root As XmlNode = doc.DocumentElement
Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner")
nsmgr.AddNamespace("pf", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition")
然后在代码中我想对我传递 lst 作为 XmlNode 的行执行某些操作并执行以下操作:
' FIRST FIND THE <TableRows> NODE, UNDER MY CURRENT <Header> ELEMENT
Dim TableRowsNode As XmlNode = lst.Item("TableRows") ' where lst is the <Header> element of the table in the rdlc i want to process
' GET ALL THE ROWS WITHIN THE <TABLEROWS> NODE
Dim Rows As XmlNodeList = TableRowsNode.SelectNodes("//rd:TableRow", ns) ' where ns is the above nsmgr
' FOR EACH ROW
For Each TableRow As XmlNode In Rows
' GET ALL THE CELLS
Dim TableCells As XmlNode = TableRow.Item("TableCells")
' FOR EACH CELL
For Each TableCell As XmlNode In TableCells
Dim ReportItems As XmlNode = TableCell.Item("ReportItems")
For Each ReportItem As XmlNode In ReportItems
Select Case ReportItem.Name
Case "Textbox"
txt_lst.Add(ReportItem)
Case "Image"
img_lst.Add(ReportItem)
End Select
Next ' each report item
Next ' each cell
Next ' each row
我尝试过 “./TableRow”
“//表格行”
“.//表格行”
“//rd:TableRow”
“//pf:TableRow”
和许多其他变体,但 TableRowsNode.SelectNodes(..) 行永远不会返回任何结果(count=0)
我做错了什么?
以下是 rdlc 正在加载的相关部分的几个片段:
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
以及我感兴趣的部分...
<Table Name="table2">
.....
<Header>
<TableRows>
<TableRow>
....
</TableRow>
<TableRow>
.....
</TableRow>
</TableRows>
我想要一个“TableRow”元素的 NodeList,以便我可以生成我将要生成的图像、文本框等的列表正在处理并通常做一些事情(对于那些感兴趣的人,生成数学符号图像,将资源字符串交换为相关文本等)...
我已经在这方面呆了太久了,而且肯定有一些愚蠢的简单的东西我错过了,因为所有搜索都会导致到相同的解决方案名称空间和 xpath 查询字符串...
请帮忙。
编辑: 让 xpath 查询与“//pf:TableRow”一起工作,但它返回整个文档中的所有节点,所以我放弃了,只使用了
Dim Rows As XmlNodeList = TableRowsNode.ChildNodes
编辑: 显然“//”使其从文档的顶层进行搜索,但我无法让“.//pf:TableRow”仅返回当前节点下的那些匹配节点?当我有更多时间时,我会再回来讨论这个问题。
Im struggling to return a XMLNodeList of the row elements under the TableRows Element of an RDLC file. There are many examples of namespaces being the cause, but ive tried them all and cant get any nodes back from my XPath query, though in fairness i dont really understand default/namespaces etc. i have checked the Default Namespace property in quickwatch and it says its "" ?? though i dont know why or how to set it as its readonly.
ive defined my namespaces with:
Dim doc As XmlDocument = New XmlDocument()
doc.Load(fname)
Dim root As XmlNode = doc.DocumentElement
Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner")
nsmgr.AddNamespace("pf", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition")
then later in the code where im wanting to do something with the rows i pass lst As XmlNode and do:
' FIRST FIND THE <TableRows> NODE, UNDER MY CURRENT <Header> ELEMENT
Dim TableRowsNode As XmlNode = lst.Item("TableRows") ' where lst is the <Header> element of the table in the rdlc i want to process
' GET ALL THE ROWS WITHIN THE <TABLEROWS> NODE
Dim Rows As XmlNodeList = TableRowsNode.SelectNodes("//rd:TableRow", ns) ' where ns is the above nsmgr
' FOR EACH ROW
For Each TableRow As XmlNode In Rows
' GET ALL THE CELLS
Dim TableCells As XmlNode = TableRow.Item("TableCells")
' FOR EACH CELL
For Each TableCell As XmlNode In TableCells
Dim ReportItems As XmlNode = TableCell.Item("ReportItems")
For Each ReportItem As XmlNode In ReportItems
Select Case ReportItem.Name
Case "Textbox"
txt_lst.Add(ReportItem)
Case "Image"
img_lst.Add(ReportItem)
End Select
Next ' each report item
Next ' each cell
Next ' each row
ive tried
"./TableRow"
"//TableRow"
".//TableRows"
"//rd:TableRow"
"//pf:TableRow"
and a host of other variants but the TableRowsNode.SelectNodes(..) line never returns any results (count=0)
what am i doing wrong?
here are a couple of snippets of the relevant sections of the rdlc im loading:
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
and the section im interested in...
<Table Name="table2">
.....
<Header>
<TableRows>
<TableRow>
....
</TableRow>
<TableRow>
.....
</TableRow>
</TableRows>
i want a NodeList of the "TableRow" elements so that i can generate a list of images, textboxes etc that i will be processing and generally doing stuff with (for those interested, generating math symbol images, swapping resource strings for thier associated text etc) ...
ive been at this for too long, and there must be something stupid simple im missing as all searches lead to the same solution namespaces, and the xpath query string...
please help.
Edit:
got the xpath query to sort of work with "//pf:TableRow" but it was returning all nodes from the entire doc, so i gave up and just used
Dim Rows As XmlNodeList = TableRowsNode.ChildNodes
Edit:
apparently the "//" makes it search from the top level of the doc, but i cant get ".//pf:TableRow" to return only those matching nodes under the current one? ill come back to this later when i have more time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是xpath标签最常见的常见问题。搜索“xpath 默认命名空间”,您会发现许多好的答案,包括来自 SO 的答案。
在 VB.NET 中,应该使用 XmlNamespaceManager 类 注册名称并将前缀(例如“x”)与其关联。
然后代替
使用:
对于默认命名空间中的所有元素名称,依此类推。
This is the most FAQ for the xpath tag. Search for "xpath default namespace" and you'll find many good answers, including ones from SO.
In VB.NET one should use the XmlNamespaceManager class to register a name and associate a prefix (say "x") with it.
Then instead of
use:
and so on for all names of elements that are in the default namespace.