Fill datagrid or listview from XML file

I have a well-formed XML file that I would like to populate a datagrid. I would prefer to use the AutoGenerate function for the WatToolKit datagrid, but could hardcode the columns.

The problem I ran into is getting the contents of the xml file in the datagrid. I partially worked on the list, but I think the datagrid will be more suitable for my needs.

Can someone give a quick example of how to do this?

+1
source share
2 answers

Yeah! I finally worked through another post here. Here's what I could get by adding each XML element to the 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 });
}
+1

XML ListView :

// 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 :

<root>  
    <element location="here" state="now"/>  
    <element location="there" state="then"/>  
</root> 
+1

Source: https://habr.com/ru/post/1712975/


All Articles