Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a couple of ways you can load up a DataGrid with XML (there are others as well):</p> <ol> <li>Using an <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.xmldataprovider.aspx" rel="nofollow">XmlDataProvider</a></li> <li>Reading the XML in from the code-behind</li> </ol> <p>Here's a very crude sample that uses both methods.</p> <p><strong>XAML</strong></p> <pre><code>&lt;Window x:Class="WpfApplication1.MyDataGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MyDataGrid" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;XmlDataProvider x:Key="BookData" Source="C:\Somewhere\Books.xml" XPath="Data"/&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;DataGrid ItemsSource="{Binding Path=Elements[Book]}" AutoGenerateColumns="False" Height="Auto" Name="dataGrid1" VerticalAlignment="Top" HorizontalAlignment="Stretch"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Author" Binding="{Binding Path=Element[Author].Value}"/&gt; &lt;DataGridTextColumn Header="Title" Binding="{Binding Path=Element[Title].Value}"/&gt; &lt;DataGridTextColumn Header="Version" Binding="{Binding Path=Element[Version].Value}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;DataGrid DataContext="{StaticResource BookData}" ItemsSource="{Binding XPath=Book}" AutoGenerateColumns="False" Height="Auto" Name="dataGrid2" Margin="0,25,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Author" Binding="{Binding XPath=Author}"/&gt; &lt;DataGridTextColumn Header="Title" Binding="{Binding XPath=Title}"/&gt; &lt;DataGridTextColumn Header="Version" Binding="{Binding XPath=Version}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p><strong>Code Behind</strong></p> <pre><code>using System.Windows; using System.Xml.Linq; namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for MyDataGrid.xaml /// &lt;/summary&gt; public partial class MyDataGrid : Window { public MyDataGrid() { InitializeComponent(); var xml = XDocument.Load( "c:\\Somewhere\\Books.xml" ).Root; dataGrid1.DataContext = xml; } } } </code></pre> <h2>For Reference</h2> <p>Finally, here are a couple of articles:</p> <ol> <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.xpath.aspx" rel="nofollow">Binding.XPath Property on MSDN</a></li> <li><a href="http://msdn.microsoft.com/en-us/magazine/cc700358.aspx#id0190068" rel="nofollow">Customize Data Display with Data Binding and WPF > Using XML Data</a></li> <li><a href="http://blogs.msdn.com/b/wpfsdk/archive/2008/11/25/a-datagrid-sample-using-xml-data.aspx" rel="nofollow">A DataGrid sample using XML data</a></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload