将多个xml文件加载到datagrid中

时间:2016-11-13 13:10:40

标签: c# wpf wpfdatagrid

我有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>

1 个答案:

答案 0 :(得分:1)

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

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