Using xpath and rdlc report

I will try to explain the problem very clearly. I am a MicroSoftReportViewer user, where I upload my report. But before downloading, I want to change something. So far, everything is in order. I want to use xpath, but when I load the rdlc (xml) file using XMLDocument, the xpath expression does not work. The only xpath that works is \ "witch gets root. I opened the file with notepad and saw that the first xml node uses these schemes

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

I tried to read the file using XMLReader with the addition of XMLSchema, but xpath does not work. Please, I will be very grateful that you will receive a code to see how to download the file, so xpath works.

Best regards, Jordan

+4
source share
1 answer

I'm afraid that we will need to make sure your XPath statement is certain, but I think this is a problem with namespaces.

Elements that do not have a prefix are located in the default namespace , which sets it to http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition for the above document.

In XPath queries, you now need to include these namespaces in your queries. This way selectSingleNode ( /elementnameicanseeinnotepad ) will not give you anything.

To add namespaces to the query, you will need to use the XmlNamespaceManager class (or use the detailed XPath syntax, which I do not recommend).

 // 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 ); 

Hope this helps.

+5
source

All Articles