Using XML File as Data Source

To use XML document as data source you need to perform the following steps:

 

  1. Create PerpetuumSoft.Reporting.Data.XmlDataSource object.
  2. Set path to XML file in the DataFile property.
  3. Set initial path (expression in XPath language) applied to XML data in the XPath property.
  4. Set created object as data source for ReportManager.

 

You can do it by using ReportManager.DataSources.Add(“DataSourceName”, “DataSource”) method.

You can use this data source in the template as any other data source.

Data source will contain as many records as there are elements found in the initial XML document by the set initial path (XPath property).

To get data for the report template use GetData(“SourcePath”) method.

Here, SourcePath is a string specifying data source name and then coma separated path to elements in accordance with XML document structure.

If SourcePath sets path to XML tag attribute, then GetData gets string value of this attribute. To get text contained inside XML tag, in the SourcePath property, set path to the tag and then coma separated “GetText()”.

Let’s review a sample:

XML document containing data on books is used as data source.

It is necessary to create report displaying data on all books. Here are the required steps:

 

  1. Create XmlDataSource data source, set this XML file in the DataFile property, and “book”(XPath=’bookstore/book’) path in the XPath property.
  2. Add created data source to the data source collection under “XmlSource” name by using the ReportManager.DataSources.Add method.
  3. Create report template containing DataBand, set data source name for it DataBand.DataSource = “XmlSource”.
  4. To display data on every book (“book”), add Detail inside DataBand.
  5. Add TextBox elements inside Detail to view various data on book. For example, to view book title (“title” element), set TextBox.Value(property binding) = ‘GetData(“XmlSource.title.GetText()”)’. To view data on author name, set 'GetData(“XmSource.author.first-name.GetText()”)’. To view data from XML attribute, for example, from ‘publicationdate’, set 'GetData(“XmSource.publicationdate”)’.

 

Report template should look as follows:

As a result you get the following document:

In some cases it is necessary to set XML document as data source directly in the template designer. There is such a possibility (review section “Direct access to data base”).

Add Feedback