A simple of Using XML Data Source type and Querying a Web Service with Reporting Services 2008

This article will demo using an XML Data Source type in Reporting Services 2008. We will then consume three different web service methods – simple web service method, parameter based method and a method that returns a dataset. 

Overview:

The Reporting Services, XML Data Provider extension, allows for you to query Web Services directly by parsing the XML structure of the SOAP response. You will need to have working knowledge of the Web Service Namespace, Method, SOAP Action, Parameters, and Schema of the response body. Once we have created our data source we can then specify what data is needed by using the XML Data Provider Query language, from within a dataset. You can learn more about using XML and Web Service Data Sources in Reporting Services here. The code download can be found at the end of this article.

This article assumes you have the following:

  • SQL Server 2008 and Reporting Services installed
  • Microsoft Visual Studio 2008 installed
  • Experience with Reporting Services 2005/2008 
  • Experience with ASP.NET and C#
  • Experience with the ASP.NET Web Service development

Steps Taken:

  1. Create ASP.NET Web Service Project 
  2. Define the three methods we'll be using
  3. Publish the Web Service
  4. Create a new Report Server Project
  5. Add a new Data Source the uses the XML type 
  6. Create report
  7. Add Dataset's - use XML Data Provider Query language
  8. Run report

Create ASP.NET Web Service

1) Launch Microsoft Visual Studio 2008

2) Select File >> New >> Web site...

reporting builder 2.0

3) Select the ASP.Net Web Service Template

4) For the Location, enter "C:\WSHost" and click OK.

5) Delete the default Service.asmx, and Service.cs from the App_Code folder.

6) Create a New Item... select the Web Service Template and name it "DemoServiceRS.asmx". Your Soultion Browser should look similar:

reporting builder 2.0

7) Open the DemoServiceRS.cs file.

8) You will notice a method named HelloWorld(), which returns the string "Hello World". We will use this for our non-parameter based Web Service Method.

9) Copy and paste the following parameter based Web Service Method, under the HelloWorld() Method:

[WebMethod]

public string GetReturnValue(string retval)

{

return retval;

}

You'll notice the [WebMethod] attribute above the method; attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service. The GetReturnValue will accept a string parameter and return that value - simple.

10) Next, add the following namespaces to your file:

using System.Data;

using System.Data.SqlClient;

This is used for the Dataset we will return in the next method.

11) Copy and paste the following code, under the GetReturnValue(string retval) method:

    [WebMethod]

    public DataSet GetTerritorySales()

    {

        string sqlText = "SELECT [TerritoryID],[Name],[CountryRegionCode],[SalesYTD] FROM [AdventureWorks].[Sales].[SalesTerritory]";

        string sqlConn = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";

 

        DataSet ds = new DataSet();

        SqlDataAdapter daTerritory = new SqlDataAdapter(sqlText, sqlConn);

 

        daTerritory.Fill(ds, "TerritorySalesYTD");

        return ds;

    }

12) Save your work.

13) Right click your Solution Name and select "Publish Web Site".

14) The Publish Web Site dialog with prompt you to enter a Target Location... Enter http://localhost/WSHost, or the location you would like to publish this site to.

15) Click OK. Wait fot the site to complete the publishing process.

You have completed developing and publishing your Web Service, which will be used later in the demo.

Create Report Server Project

1) Create a new Report Server Project - I assume you have done this before. If not, please read our previous post Introduction to Microsoft SQL Server 2008 Reporting Services Report Builder 2.0 Part 2.

2) Name the project "DemoReportProject".

Create Shared Data Source

3) Add a new Shared Data Source and enter "AdventureWorksWSDL" for the Name. 

4) Select XML for the type.

5) For the Connection String, enter the location of the Web Service you published earlier in this article. For example, http://localhost/WSHost/DemoServiceRS.asmx. Your Shared Data Source Properties dialog should look similar:

reporting builder 2.0

6) From within the left pane, select Credentials.

7) Select the Use Windows Authentication (Integrated Security) option and click OK to close the dialog.

Create Report

1) Add a new report the project and name it "WSQuery.rdl".

Add Dataset's

Non-Parameter Based Query

1) Add a new dataset and name it HelloWorld

2) Within the dataset, select AdventureWorkdWSDL for the Data Source

3) Copy and paste the following script to the Query field:

<Query>
<Method Namespace="http://tempuri.org/" Name="HelloWorld"/>
<SoapAction>http://tempuri.org/HelloWorld</SoapAction>
</Query>

Note: The Method element requires you to pass the Web Service Method to be consumed. Notice the Namespace passed in the example above. You have the option to rename this in your ASP.NET Web Service project. The XML Data Provider will auto-generate SOAP Action by appending Method Name and Namespace. For non-.NET Framework Web services, the SOAP Action may differ and will have to be set explicitly in the query.

reporting builder 2.0

4) Click OK.

5) You should see the HelloWorld Dataset in the Report Data pane.

6) Add a Matrix to the report.

7) Drag and drop the HelloWorldResult field to the matrix.

8) Run the report. You should see something similar:

reporting builder 2.0

Parameter Based Query

1) Add a new Dataset from the Report Data pane.

2) Name the new Dataset "GetReturnValue".

3) Within the dataset, select AdventureWorkdWSDL for the Data Source.

4) Copy and paste the following code Query field:

<Query>
<SoapAction>http://tempuri.org/GetReturnValue</SoapAction>
<Method Namespace="http://tempuri.org/" Name="GetReturnValue">
   <Parameters>
        <Parameter Name="retval" Type="String">
  <DefaultValue>Test</DefaultValue>
 </Parameter>
   </Parameters>
</Method>
<ElementPath IgnoreNamespaces="true">*</ElementPath>
</Query>

Note: You will notice a couple of new elements in this query - Parameters and ElementPath. Keep in mind, Parameters are case-sensitive. An ElementPath indicates how to traverse the XML hierarchical nodes and their attributes in the XML data source. If IngnoreNamespaces is set to true, namespaces in the XML ElementPath and the XML document are ignored.

Report Parameter

5) Add a new Parameter; From the Report Data pane, right click the Parameters folder and click Add Parameter...

6) Enter "retval" for the Name value.

7) Enter "Parameter Value:" for the Prompt value.

8) Make sure the Data type is set to Text.

9) Click OK to save Parameter changes.

Dataset Parameter

10) From within the Report Data pane, right click the GetReturnValue dataset and select Dataset Properties.

11) Click the Parameters pane item on the left.

12) Click Add from the Choose Query Parameter values section.

13) Under the Parameter Name column, enter "retval" (without the quotes).

14) Under the Parameter Value column, use the dropdown to select [@retval].

15) Click OK.

Add Matrix

16) Add a Matrix to the report.

17) Drag and drop the GetReturnValueResponse field to the matrix.

Note: If you attempt to run the report, you may not see a rendered value for GetReturnValueResponse, after entering the Parameter Value and clicking View Report.

Common error: The dataset ‘GetReturnValue’ contains a definition for the Field ‘GetReturnValueResponse’. This field is missing from the returned result set from the data source.
[rsErrorReadingDataSetField] The dataset ‘GetReturnValue’ contains a definition for the Field ‘GetReturnValueResponse’. The data extension returned an error during reading the field. There is no data for the field at position 2.

Resolution: This is due to the Field Source having the incorrect value. We need to change the Field Source from GetReturnValueResponse to GetReturnValueResult.

18) Under the GetReturnValue dataset, right click GetReturnValueResponse and select Field Properties.

19) Find the GetReturnValue row and change value under the Field Source column to "GetReturnValueResult".

reporting builder 2.0

20) Click OK and Save your work.

21) Run the report. You should see something similar:

reporting builder 2.0

You will notice the new Parameter field at the top. You may enter any value for this field and it should render to the report.

Return Dataset Query 

1) Add a new Dataset from the Report Data pane.

2) Name the new Dataset "TerritorySalesYTD".

3) Within the dataset, select AdventureWorkdWSDL for the Data Source.

4) Copy and paste the following code to the Query field:

<Query>
     <SoapAction>http://tempuri.org/GetTerritorySales</SoapAction>
     <Method Namespace="http://tempuri.org/" Name="GetTerritorySales"></Method>
     <ElementPath IgnoreNamespaces="true">GetTerritorySalesResponse{}/GetTerritorySalesResult{}/diffgram{}/NewDataSet{}/TerritorySalesYTD{TerritoryID, Name, CountryRegionCode, SalesYTD}</ElementPath>
</Query>

Note: You will notice the ElementPath has some new values. In short, the values passed here will return what fields we want returned from the dataset. You can learn more about XML and Web Service Data Sources here.

Also, if you are uncertain of the XML Schema, you may run this query in the Query Designer, but exculed everything between the <ElementPath IngnoreNamespaces="true"></ElementPath>. This will return the Dataset ID, Field Names, Data Types, etc. Once you have this information you may construct the element path accordingly.

5) Click OK.

6) Add a Matrix to the report.

7) Drag and drop the following fileds to the matrix: CountryRegionCode, Name and SalesYTD.

8) Save your work.

10) Run the report (dont' forget to pass a parameter, from the previous dataset). You should see something similar:

reporting builder 2.0

That's all there is to it!

Cheers!

Download code here: 

WSHost.zip (3.43 kb)

DemoReportProject1.zip (32.67 kb)

原文地址:https://www.cnblogs.com/Jayan/p/1723946.html