views:

2506

answers:

3

I have a well formed XML file I would like to fill a datagrid with. I would prefer using the AutoGenerate feature of WFPToolKit datagrid, but could hard code the columns in.

The trouble I am having is getting the xml file contents into a datagrid. I had it partially working in a listview, but think a datagrid would be more suited for my needs.

Can anyone provide a quick example of how to accomplish this?

Thanks!

A: 

Aha! I finally worked it out with the help of another post here. Here is what I was able to get working, adding each XML element to a list view.

XDocument xdoc = XDocument.Load("c:\\isbn.xml");
        var items = from item in xdoc.Descendants("BookData")
                    select new
                    {
                        Title = item.Element("Title").Value,
                        AuthTexts = item.Element("AuthorsText").Value
                    };
        foreach (var item in items)
        {
            listView1.Items.Add(new { Title = item.Title, Author = item.AuthTexts });
        }
Dave
A: 

I bound the XML to the ListView like this:

// Bind the data to the ListView
var binding = new System.Windows.Data.Binding() {  
  Source = MergedXmlDataProvider,  
  UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,  
  XPath = "//element" };  
this.listView1.SetBinding(ItemsControl.ItemsSourceProperty, binding);

XML looks something like this:

<root>  
    <element location="here" state="now"/>  
    <element location="there" state="then"/>  
</root>
Number8
A: 

Thanks Number8. You have pretty much saved a possible loss of about 4 more days of non-productivity! I was trying to dynamically load an XML document and only bind two columns. With your code I was able to achieve that!

evoFreak