使用 XML 中未知数量的数据填充 WPF DataGrid

发布于 2024-12-10 23:14:08 字数 1392 浏览 1 评论 0原文

我试图从 xml 文件中读取“Data”元素中的所有属性,并将它们显示在 DataGrid(WPF、.Net 4.0)中

。XML 的格式如下所示。

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

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

我正在努力为数据网格创建一个支持类来绑定,或者以编程方式创建它。

我当前的尝试是创建一组文本列(根据我从 xml 文件中提取的内容给出要使用的列名称列表),

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);
}

然后迭代每个属性并将其添加到列表中(以表示行在数据网格中显示)如果属性名称与标题值之一匹配,

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 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)

The XML is in the format shown below.

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2024-12-17 23:14:08

那么,您动态创建列的代码是正确的。

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

  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...

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

还有一篇文章使用 XmlDataProvider

Well your code to create the columns dynamically is correct.

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);
  } 

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

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

Also there is this article that make use of XmlDataProvider

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文