To use XML document as data source you need to perform the following steps:
- Create PerpetuumSoft.Reporting.Data.XmlDataSource object.
- Set path to XML file in the DataFile property.
- Set initial path (expression in XPath language) applied to XML data in the XPath property.
- 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:
- Create XmlDataSource data source, set this XML file in the DataFile property, and “book”(XPath=’bookstore/book’) path in the XPath property.
- Add created data source to the data source collection under “XmlSource” name by using the ReportManager.DataSources.Add method.
- Create report template containing DataBand, set data source name for it DataBand.DataSource = “XmlSource”.
- To display data on every book (“book”), add Detail inside DataBand.
- 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”).