How to bind xml to WPF DataGrid?

I searched and tried various solutions, but so far none of them have solved my problem. I am using the built-in DataGrid from WPF in Visual Studio 2010 / .NET4 to display data from an XML document stored as an XDocument.

My code is working fine, and I confirmed that the XDocument is present and correct. However, the DataGrid does not display any data.

XML looks like this (simplified for clarity):

<data> <track> <id>211</id> <name>Track Name</name> <duration>156</duration> <artist_id>13</artist_id> <artist_name>Artist Name</artist_name> <album_id>29</album_id> <album_name>Album Name</album_name> </track> ... </data> 

My XAML looks like this:

 <DataGrid x:Name="LibraryView" Grid.Row="1" DataContext="{Binding Path=TrackList}" ItemsSource="{Binding XPath=/data/track}"> <DataGridTextColumn Header="Title" Binding="{Binding XPath=name}"/> <DataGridTextColumn Header="Artist" Binding="{Binding XPath=artist_name}"/> <DataGridTextColumn Header="Album" Binding="{Binding XPath=album_name}"/> <DataGridTextColumn Header="Length" Binding="{Binding XPath=duration}"/> </DataGrid> 

C #, which supports it, simply assigns a new XDocument (downloaded from the web service) to the TrackList property (which implements INotifyPropertyChanged). Further processing is not performed on it.

I had previously tried using XLinq to bind to the result of a query that didn't work either (same problem), so I thought I'd try the XPath approach to avoid writing a potentially false Linq statement and try to find the problem.

I'm running out of ideas on how to properly display a DataGrid. My understanding of how this should work is clearly lacking, so I would really appreciate any help offered.

Edit: It is worth noting that I have some flexibility with the input format, since I load raw XML. I will try some of the suggestions and see what I can do.

+7
data-binding wpf wpfdatagrid
source share
2 answers

I used XLinq and worked fine using XElement instead of XDocument:

 XElement TrackList = XElement.Load("List.xml"); LibraryView.DataContext = TrackList; 

Xaml:

 <DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[track]}"> <DataGrid.Columns> <DataGridTextColumn Header="Artist" Binding="{Binding Path=Element[artist_name].Value}"/> <DataGridTextColumn Header="Album" Binding="{Binding Path=Element[album_name].Value}"/> <DataGridTextColumn Header="Length" Binding="{Binding Path=Element[duration].Value}"/> </DataGrid.Columns> </DataGrid> 
+9
source share

XPath binding only makes sense if you bind to what is an XmlNode (for example, you use XmlDataProvider). See here .

XPath does not work with XDocument classes. The only way to bind to XDocument properties is with the usual syntax for a path that is not known to XML.

It is best to use an XmlDataSource or convert your Xml document through XDocument to POCO. This is pretty simple using LINQ:

 XDocument doc = XDocument.Load(xmlFile); var tracks = from track in doc.Descendants("data") select new Track() { Name= track.Element("name").Value, Duration= track.Element("duration").Value, etc ... }; LibraryView.ItemsSource = tracks; 
+1
source share

All Articles