使用来自XML的未知数量的数据填充WPF DataGrid [英] Populate WPF DataGrid with unknown amount of data from XML

查看:60
本文介绍了使用来自XML的未知数量的数据填充WPF DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从xml文件中读取数据元素中的所有属性,并将其显示在DataGrid(WPF,.Net 4.0)中

I'm trying to read all of the attributes in the 'Data' elements from an xml file and display them in a DataGrid (WPF, .Net 4.0)

XML格式如下所示。

The XML is in the format shown below.

<root>
  <Data Name1="Value1" Name2="Value2" ... />
  <Data Name1="Value1" Name2="Value2" ... />
  ...
</root>

我不知道所使用的属性名称(例如Name1,Name2,Foo等)还是每个元素中包含的属性数量(每个元素的属性数量可能不同)。每个新元素都应显示在新行上,并且每个属性对应于一列(如果该行不存在,则显示null)。

I have no idea of the attribute names used (e.g. Name1, Name2, Foo etc.) or the number of attributes there are in each element (potentially different number of attributes for each element). Each new element should be displayed on a new row, and each attribute corresponds to a column (if it doesn't exist for that row display null).

我是努力创建供数据网格绑定的后备类或以编程方式创建所有后备类。

I'm struggling to create a backing class for the datagrid to bind to, or to create it all programatically.

我当前的尝试是创建一组文本列(给定一个列表)基于我从xml文件提取的内容使用的列名)。

My current attempt is to create a set of text columns (given a list of the column names to use based on what I've extracted from the xml file)

List<string> dataTags = ...; //get list of attributes to use from xml
foreach (string d in dataTags) {
    DataGridTextColumn col = new DataGridTextColumn();
    col.Header = d;
    dataGrid.Columns.Add(col);
}

我然后遍历每个属性并将其添加到列表中(以表示如果属性名称与标头值之一匹配

I then iterate through each attribute and add it to a list (to represent the row to display in the datagrid) if the attribute name matches one of the header values

foreach (XElement e in xml.Descendants("Data")) {
    //Store values to be added to the datagrid as a new row
    List<string> row = new List<string>();

    //Only add data where the attribute name matches one of the 'dataTags' we are given.
    foreach(string d in dataTags) {
        foreach (XAttribute a in e.Attributes()) {
            if (d.Equals(a.Name.ToString())) {
                row.Add(a.Value);
                break;
            }
        }
    }

    //Add new row to the datagrid
    dgData.Items.Add(row);
}

我在某种程度上...但是有一个数据网格空行(正确的行数,但现在显示的是数据)。

I'm some of the way there ... but have a datagrid with blank rows (correct number of rows but now data shown).

任何提示/指针都将受到赞赏。

Any tips/pointers would be appreciated.

推荐答案

让您的代码动态创建列是正确的。

Well your code to create the columns dynamically is correct.

但是您在数据网格中添加项目的代码不正确。为此,在配置列时,将XPATH分配给它们。

But your code to add items in the data grid is not correct. For that while you configure your columns, assign XPATH to them.

  List<string> dataTags = ...; //get list of attributes to use from xml 
  foreach (string d in dataTags)
  {
         DataGridTextColumn col = new DataGridTextColumn();
         col.Binding = new Binding() { XPath = "@" + d } ;
         col.Header = d;
         dataGrid.Columns.Add(col);
  } 

,然后将XmlNodes作为ItemsSource分配给datgrid ...

and then assign your XmlNodes as ItemsSource to the datgrid...

  dataGrid.ItemsSource = xml.Descendants("Data");

另外,本文还利用 XmlDataProvider

这篇关于使用来自XML的未知数量的数据填充WPF DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆