1
votes

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.

1
If there is no overwriting declaration of default namespace, then //pf:TableRow shoould work with pf prefix bound to http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition namespace URI - user357812
Good question, +1. see my answer for specific instructions. :) - Dimitre Novatchev

1 Answers

1
votes

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

TableRow

use:

x:TableRow

and so on for all names of elements that are in the default namespace.