Skip to main content Skip to footer

ActiveReports7: Binding To REST Web Service Data

A RESTful web service (also called a RESTful web API) is a web service implemented using HTTP and the principles of REST. It is a collection of resources, with four defined aspects:

  • The base URI for the web service
  • The Internet media type of the data supported by the web service. This is often XML but can be any other valid Internet media type provided that it is a valid hypertext standard.
  • The set of operations supported by the web service using HTTP methods (e.g., GET, PUT, POST, or DELETE).
  • The API must be hypertext driven.

This blog discusses how we can bind XML data returned by the RESTful web service to Active Reports 7. In this implementation, Report is bound to a datasource at runtime for which we can use the DataSet provider or the Object provider with the LocateDataSource event. The reporting engine raises the LocateDataSource event when it needs data to be rendered. Refer to this link for more information on how to use the Dataset provider. So, now we have the XML data returned from the REST Web service which is simply a URL displaying the XML data, and the Dataset provider that would help us to bind the report to an unbound datasource at runtime. First we load the XML data from the URL link into an XmlDocument using its Load method. Then we need to create a Datatable with all the fields that would be returned by the XML data. Once this is done, we can iterate through the XmlDocument to fill the Datatable with appropriate values for the fields. Following code block shows the implementation.



      XmlDocument _doc = new XmlDocument();  
      dt = new DataTable();  
      dt.Columns.Add(new DataColumn("Title", typeof(String)));  
      dt.Columns.Add(new DataColumn("Artist", typeof(String)));  
      dt.Columns.Add(new DataColumn("Country", typeof(String)));  
      dt.Columns.Add(new DataColumn("Company", typeof(String)));  
      dt.Columns.Add(new DataColumn("Price", typeof(Double)));  
      dt.Columns.Add(new DataColumn("Year", typeof(Int32)));  
      \_doc.Load("http://www.w3schools.com/xml/cd\_catalog.xml");  
      XmlElement root = _doc.DocumentElement;  
      XPathNavigator navigator = _doc.CreateNavigator();  
      XPathNodeIterator cdData = navigator.Select("//CD");  
      while (cdData.MoveNext())  
      {  
         XPathNodeIterator cdDataElements = cdData.Current.SelectChildren(XPathNodeType.Element);  
         DataRow dr = dt.NewRow();  
         while (cdDataElements.MoveNext())  
          {  
             //Read the values based on the element name  
                switch (cdDataElements.Current.Name)  
                {  
                     //convert values as necessary to match the business object's property types  
                     case "TITLE": dr["Title"] = cdDataElements.Current.Value.ToString();  
                                   break;  
                     case "ARTIST": dr["Artist"] = cdDataElements.Current.Value.ToString();  
                                    break;  
                     case "COUNTRY": dr["Country"] = cdDataElements.Current.Value.ToString();  
                                     break;  
                     case "COMPANY": dr["Company"] = cdDataElements.Current.Value.ToString();  
                                     break;  
                     case "PRICE": dr["Price"] = Convert.ToDouble(cdDataElements.Current.Value);  
                                   break;  
                     case "YEAR": dr["Year"] = Convert.ToInt32(cdDataElements.Current.Value);  
                                  break;  
                 }  
            }  
         dt.Rows.Add(dr);  
      }  

Once we have the datatable filled with all the data, we can return the same when the LocateDataSource event is raised to find the data for input into the report. Here is the code for the same:



private void \_runtime\_LocateDataSource(object sender, LocateDataSourceEventArgs args)  
{  
  Object data = null;  
  if (StringsAreEqual("DataSource1", args.DataSourceName))  
  {  
    if (StringsAreEqual("DataSet1", args.DataSetName))  
    {  
       data = dl.GetData;  
    }  
  }  
}  


Download the attached samples for complete implementation. XmlBindSource C# XmlBindSource Vb

MESCIUS inc.

comments powered by Disqus