xtrareports-5153-feature-guide-to-devexpress-reports-bind-reports-to-data-xml-file-bind-a-report-to-an-xml-file-runtime-sample.md
This example illustrates how to bind a report to XML data stored in a file (Cars.xml in this case).
Do the following to accomplish this task:
Create a SqlDataSource instance.
Create a SelectQuery to access data from the required data table.
Use the SelectQueryFluentBuilder.AddTable method to include the required table in the query.
Specify columns to include in the table using the SelectQueryFluentBuilder.SelectColumns method.
Imports System
Imports System.Windows.Forms
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraReports.UI
' ...
Private Function CreateReport() As XtraReport
' Create a new report instance.
Dim report As New XtraReport()
' Create a data source.
Dim ds As New SqlDataSource(New XmlFileConnectionParameters("../../Cars.xml"))
' Create a query to access fields of the Cars data table.
Dim query As SelectQuery = SelectQueryFluentBuilder.
AddTable("Cars").
SelectColumns("ID", "Trademark", "Model", "Category", "Description").
Build("MyQuery")
ds.Queries.Add(query)
' Make the data source structure displayed
' in the Field List of an End-User Report Designer.
ds.RebuildResultSchema()
' Assign the data source to the report.
report.DataSource = ds
report.DataMember = "MyQuery"
' Add a detail band to the report.
Dim detailBand As New DetailBand()
report.Bands.Add(detailBand)
' Add a label to the detail band.
Dim label As XRLabel = New XRLabel With {.WidthF = 300}
'label.DataBindings.Add("Text", Nothing, "customQuery1.ProductName")
label.Text = "[MyQuery.Trademark] [MyQuery.Model]"
label.LeftF = 30
detailBand.Controls.Add(label)
detailBand.Height = CInt(label.HeightF)
' Create a group header band and add it to the report.
Dim ghBand As New GroupHeaderBand()
report.Bands.Add(ghBand)
' Create a grouping criteria.
ghBand.GroupFields.Add(New GroupField("Category"))
' Display the grouping criteria in the group header.
Dim labelGroup As New XRLabel()
labelGroup.Text = "[MyQuery.Category]"
ghBand.Controls.Add(labelGroup)
ghBand.Height = CInt(labelGroup.HeightF)
Return report
End Function
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
' Show the report's print preview.
Dim printTool As New ReportPrintTool(CreateReport())
printTool.ShowPreview()
End Sub
Private Sub button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Open the report in an End-User Report Designer.
Dim designTool As New ReportDesignTool(CreateReport())
designTool.ShowRibbonDesignerDialog()
End Sub
using System;
using System.Windows.Forms;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraReports.UI;
// ...
XtraReport CreateReport() {
// Create a new report instance.
XtraReport report = new XtraReport();
// Create a data source.
SqlDataSource ds = new SqlDataSource(new XmlFileConnectionParameters("../../Cars.xml"));
// Create a query to access fields of the Cars data table.
SelectQuery query = SelectQueryFluentBuilder
.AddTable("Cars")
.SelectColumns("ID", "Trademark", "Model", "Category", "Description")
.Build("MyQuery");
ds.Queries.Add(query);
// Make the data source structure displayed
// in the Field List of an End-User Report Designer.
ds.RebuildResultSchema();
// Assign the data source to the report.
report.DataSource = ds;
report.DataMember = "MyQuery";
// Add a detail band to the report.
DetailBand detailBand = new DetailBand();
report.Bands.Add(detailBand);
// Add a label to the detail band.
XRLabel label = new XRLabel { WidthF = 300 };
label.Text = "[MyQuery.Trademark] [MyQuery.Model]";
label.LeftF = 30;
detailBand.Controls.Add(label);
detailBand.Height = (int)label.HeightF;
// Create a group header band and add it to the report.
GroupHeaderBand ghBand = new GroupHeaderBand();
report.Bands.Add(ghBand);
// Create a grouping criteria.
ghBand.GroupFields.Add(new GroupField("Category"));
// Display the grouping criteria in the group header.
XRLabel labelGroup = new XRLabel();
labelGroup.Text = "[MyQuery.Category]";
ghBand.Controls.Add(labelGroup);
ghBand.Height = (int)labelGroup.HeightF;
return report;
}
private void button1_Click(object sender, EventArgs e) {
// Show the report's print preview.
ReportPrintTool printTool = new ReportPrintTool(CreateReport());
printTool.ShowPreview();
}
private void button2_Click(object sender, EventArgs e) {
// Open the report in an End-User Report Designer.
ReportDesignTool designTool = new ReportDesignTool(CreateReport());
designTool.ShowRibbonDesignerDialog();
}
Note
Use the XtraReportBase.XmlDataPath property to bind reports to XML documents that do not contain an embedded schema definition in XSD format.
See Also