How to: Bind a Chart to an XML Data Source using the XPath Property

The following example demonstrates how to bind a chart to an XML data source using the XPath property.

To accomplish this task, it is necessary to add an XmlDataProvider object to a collection of the window's static resources, then assign this resource to a series' Series.DataSource property using the XPath key to define the path to a specific XML node. Then, set the Series.ArgumentDataMember and Series.ValueDataMember properties to the names of XML elements that should provide data for arguments and values.

Note that this approach to data binding is done completely in XAML, and no code-behind file is required.

<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" Title="Window1" Height="491" Width="637">
    <Window.Resources>
        <ResourceDictionary>
            <XmlDataProvider x:Key="contacts" Source="Contacts.xml" XPath="/NewDataSet">
            </XmlDataProvider>
        </ResourceDictionary>
    </Window.Resources>
        <Grid>
        <dxc:ChartControl Name="chartControl1">
            <dxc:ChartControl.Diagram>
                <dxc:XYDiagram2D>
                    <dxc:XYDiagram2D.Series>
                        <dxc:BarSideBySideSeries2D DataSource="{Binding Source={StaticResource contacts}, XPath=Contacts}" ArgumentDataMember="LastName" ValueDataMember="PaymentAmount" />
                    </dxc:XYDiagram2D.Series>
                </dxc:XYDiagram2D>
            </dxc:ChartControl.Diagram>
        </dxc:ChartControl>
    </Grid>
</Window>