How to: Bind a Chart to an XML Data Source

  • 2 minutes to read

This tutorial demonstrates how a chart can be bound to an XML data source. In this example, we'll bind a chart to the Departments.xml file, shipped with the XtraCharts suite and located in the directory, where you installed DevExpress demos.

To bind a chart to an XML data source, do the following.

  1. Start MS Visual Studio (2008 or 2010), and create a new Windows Forms Application, or open an existing one.
  2. Add a chart to the form.
  3. Add Bar series to the chart.
  4. Handle the Form1_Load event using the following code.

    
    using System;
    using System.Data;
    using System.Windows.Forms;
    using DevExpress.XtraCharts;
    // ...
    
    private void Form1_Load(object sender, EventArgs e) {
        // Create a DataSet instance and, make it read the xml file.
        DataSet ds = new DataSet();
        ds.ReadXml(@"..\..\Departments.xml");
    
        // Specify the dataset as the chart's data source.
        chartControl1.DataSource = ds;
        // Specify the argument and value data members for the series. 
        chartControl1.Series[0].ArgumentDataMember = "Table.Department";
        chartControl1.Series[0].ValueDataMembers.AddRange(new string[] { "Table.Budget" });
        // Define an appropriate scale type for the series points' values.
        chartControl1.Series[0].ValueScaleType = ScaleType.Numerical;
    }
    

The result is shown in the following image.

HowTo_XMLdataSource

TIP

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E1583.