none
Editing RDLC XML using XMLDocument RRS feed

  • Question

  •  

    Hi,

     

    This is one of those help me help myself questions (rare I'm sure).  But that doesn't mean I won't cheat and include my problem!  : >)

     

     

    Can someone please link me to, or explain the syntax used when using XMLDocument.SelectNodes(....).  Actually, what I'm really trying to find out is how to use the XML syntax in general.

     

    I've seen tons of examples like the following . . .

    Code Snippet

     

    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(RDLCdoc.NameTable)

    nsmgr.AddNamespace("ms", http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition)

     

    Dim HeadNode As XmlNode = RDLCdoc.DocumentElement

    Dim TableNodeList2 As XmlNodeList = HeadNode.SelectNodes("/Body/ReportItems", nsmgr)

     

     

     

    So I load the XML definition into the namespacemanager (I'm not sure why, mind you, but I really want to understand what is going on here - doesn't the structure of the file provide the needed Node/Name/Data structural relationships?).

     

    Then I pass that to my SelectNodes method with an "xpath". Here I know my example is wrong (the fact that it doesn't work being a great indicator).

     

    For example, in another post the RDLC maven Lisa Nichols used a string of characters when searching for a "single node".  I'm guessing I need a similer syntax, but again, I'm not sure what that syntax is.

     

    Code Snippet

    y = x.SelectSingleNode("/*/*[local-name() = 'PageHeight']")

     

     

     

    So in short, can anyone point me to an xml primer that will explain this stuff.  I've found a few basic ones, but none were .Net specific, nor did they cover this namespace suff and accompanying syntax for the XML___ classes.  I'm lost when it comes to the syntax when specifying xml nodes.

    Tuesday, April 15, 2008 11:31 PM

Answers

  • Finally a break through:

     

    "Apparently, the default namespace does not work with the namespace
    manager"

     

    I would love to hear comments on the possibility of the namespace manager being broken . . .

     

    http://www.ureader.com/message/33244880.aspx

     

    compliments of victor:

     

    Apparently, the default namespace does not work with the namespace
    manager, so you have to give the default name space a name, for
    example ns.

    Then your xpath questions work, if you write them like this:

            XmlNamespaceManager ns = new
    XmlNamespaceManager(pg1.NameTable);
            ns.AddNamespace("ns", "http://schemas.microsoft.com/sqlserver/
    reporting/2005/01/reportdefinition");
            ns.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/
    reporting/reportdesigner");

            string source = "//ns:Report/ns:Body/ns:ReportItems/
    ns:Rectangle[@Name='rctPage']/ns:ReportItems";

            MessageBox.Show(pg1.SelectSingleNode(source, ns).InnerXml);

    Notice that you should not add the namespace identifier for
    attributes, see @Name above.  Very strange, but it works.

     

    Wednesday, April 16, 2008 4:22 PM

All replies

  • Hi again,

     

    I just wanted to mention that I did read

    http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=312

     

    which has an example of

     

    HeadNode.SelectNodes("/Body/ReportItems", nsmgr)

     

    but again, mine did not work (is there a problem with my path or something more)?

     

    And I'm still baffled about the syntax issue, but I'll keep hunting.

    Tuesday, April 15, 2008 11:36 PM
  • I haven't done this myself yet, read a bit on it, but first it looks like you only have one namespace being added, and you should have two,  open an .RDLC in it's XML format, and notice the namespaces at the top.

     

    Lisa Nichols has a lot to say about this stuff, so go check out her articles and threads.

    Wednesday, April 16, 2008 1:34 PM
  • Thanks for the tip. I added the second namespace, but still no luck.

     

    I checked out Lisa's blog but she reports mainly on visual fox pro there.

     

    I found more information on the syntax of xpaths

     

    http://www.codeproject.com/KB/cpp/myXPath.aspx

     

     

    But still no luck! 

     

     

    my latest failed attempt took a different approach per the linked article, but frankly, I don't fully understand the difference between an XMLDocument, and an XPathNavigator.  (Though apparently the XMLDocument uses more memory - but I'm sure there's a positive side to that tradeoff.)

     

    In either case, it doesn't work . . .

    Code Snippet

     

    Dim HeadNode As XmlNode = RDLCdoc.DocumentElement 'Head Node is report

    Dim nav As XPathNavigator = HeadNode.CreateNavigator

    Dim BodyNode As Boolean = nav.MoveToChild("Body", "Body")

    Dim xpathExpr As XPathExpression = nav.Compile("/Body/ReportItems/Table")

    Dim iterator As XPathNodeIterator = nav.Select(xpathExpr)

    Dim TableNodeList2 As XmlNodeList = HeadNode.SelectNodes("/Body/ReportItems/Table", nsmgr)

     

    Dim TableNodeList2 As XmlNodeList = HeadNode.SelectNodes("/Body/ReportItems/Table", nsmgr)

     

     

    This returns 0 entries in the node list.
    Wednesday, April 16, 2008 4:06 PM
  • Finally a break through:

     

    "Apparently, the default namespace does not work with the namespace
    manager"

     

    I would love to hear comments on the possibility of the namespace manager being broken . . .

     

    http://www.ureader.com/message/33244880.aspx

     

    compliments of victor:

     

    Apparently, the default namespace does not work with the namespace
    manager, so you have to give the default name space a name, for
    example ns.

    Then your xpath questions work, if you write them like this:

            XmlNamespaceManager ns = new
    XmlNamespaceManager(pg1.NameTable);
            ns.AddNamespace("ns", "http://schemas.microsoft.com/sqlserver/
    reporting/2005/01/reportdefinition");
            ns.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/
    reporting/reportdesigner");

            string source = "//ns:Report/ns:Body/ns:ReportItems/
    ns:Rectangle[@Name='rctPage']/ns:ReportItems";

            MessageBox.Show(pg1.SelectSingleNode(source, ns).InnerXml);

    Notice that you should not add the namespace identifier for
    attributes, see @Name above.  Very strange, but it works.

     

    Wednesday, April 16, 2008 4:22 PM