将多个XML文件加载到数据网格

本文关键字:数据 数据网 网格 加载 文件 XML | 更新日期: 2023-09-27 18:17:53

我有一个xml文件列表。(从一个目录中读取)我想在datagrid中显示每个xml文件中存在的所有记录。

下面是相同(xaml文件)的代码

  <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Elements[TestResult]}"  x:Name="dataGrid" Margin="10,0,-1,0">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="TestCaseId" Binding="{Binding Path=Attribute[TestCaseId].Value}" />
                            <DataGridTextColumn Header="Outcome" Binding="{Binding Path=Attribute[Outcome].Value}"/>
                            <DataGridTextColumn Header="Duration" Binding="{Binding Path=Attribute[Duration].Value}"/>
                            <DataGridTextColumn Header="Comment" Binding="{Binding Path= Attribute[Comment].Value}"/>
                        </DataGrid.Columns>
                    </DataGrid>
TestREsult Class
class TestResult
{      
        [XmlAttribute("TestCaseId")]
        public string TestCaseId { get; set; }
        [XmlAttribute("Outcome")]
        public string Outcome { get; set; }
        [XmlAttribute("Duration")]
        public string Duration { get; set; }
        [XmlAttribute("Comment")]
        public string Comment { get; set; }
    }
Here is the code for loading each file in to datagrid 

但是我只能看到最后一个文件数据

  foreach (string item in Files)
        {
           var peopleList = XElement.Load(item);
           this.dataGrid.DataContext = peopleList;
        }

这是一个xml文件

 <TestResults URL="https://google.com/" StartDateTime="2016-11-11T14:52:25.2499848+05:30">
<TestResult TestCaseId="Smoke_21949" Outcome="Pass" Duration="00:03:09.9335101" Comment="" />
<TestResult TestCaseId="Smoke_31234" Outcome="Pass" Duration="00:02:56.1503179" Comment="" />
</TestResults>

将多个XML文件加载到数据网格

您需要创建一个列表并添加到列表中,

var peopleList = new List<XElement>();
foreach (string item in Files)
    {
       peopleList.Add(XElement.Load(item));           
    }
this.dataGrid.DataContext = peopleList;