Binding to an XML File

Glossary Item Box

Language

Visual Basic

C#

JScript

Show All

Languages Infragistics(R) NetAdvantage(R) for WPF

Binding to an XML File

This topic shows you how to declare an XmlDataProvider object in XAML. The XAML code for declaring the XmlDataProvider is written generically enough that it is control-independent, and no mention of a data-displaying control is made until the "Use the Data Provider" section of this topic. The XML file used in this topic is called Orders, and is available for you to download. The example XAML code assumes the XML file exists in a folder called "Data" in your project.

Set Up the Data Provider

  1. Create a Microsoft® Windows® Presentation Foundation Window or Page project.
  2. Create a Resources section in which to define the XmlDataProvider. 

    In XAML:

    <Grid.Resources>
    	...
    </Grid.Resources>
  3. Within the Resources section declared in step 1, you will declare an XmlDataProvider. The following example code sets the XmlDataProvider's Source property to the Orders.xml file. A key is also assigned to the XmlDataProvider so that it can be referenced later. The final property set is XPath, which is an XPath query used to generate the data collection.

    In XAML:

    ..
    	<XmlDataProvider Source="../Data/Orders.xml" 
    	  x:Key="OrderData" XPath="/Orders" />
    ...
    

Use the Data Provider

At this point you have everything in place to read in your Xml file. Now let's talk about how to display the file's contents inside one of the NetAdvantage for WPF data-driven controls. The control options include xamDataCarousel™, xamDataGrid™, xamDataPresenter™, and xamCarouselListBox™. With the exception of xamCarouselListBox, connecting the data set to the control is as simple as setting the control's DataSource property to a binding expression. For xamCarouselListBox, you need to set the ItemsSource property to the binding expression. 

The following steps show you how to assign xamDataGrid's DataSource property to a binding expression that makes use of the XmlDataProvider declared in the procedure, "Set Up the Data Provider".

  1. For easier access to xamDataGrid, place the following namespace declaration within the opening Page or Window tag.

    In XAML:

    xmlns:igDP="http://infragistics.com/DataPresenter"
    

  2. Use the following XAML code to create an instance of XamDataGrid, name it, and set its DataSource property to the XmlDataProvider you created above using a binding expression.

    In XAML:

    <igDP:XamDataGrid x:Name="XamDataGrid1" 
      DataSource="{Binding Source={StaticResource OrderData}, XPath=Order}"/>
    

  3. Build and run the project. You should see the xamDataGrid control populated with data similar to the image below.

E-mail your feedback on this topic.

Opinion about our help? Take our survey.

Copyright © 2003-2007 Infragistics, Inc. All rights reserved.

Build Version: 7.1.20071.1320