[英]How to bind dataset from XML to ItemSource in C# WPF?
I am trying to bind a dataset from XML to an ItemSource
and do not get it right. 我试图将数据集从XML绑定到ItemSource
,但没有正确处理。
Here is the .xaml
part: 这是.xaml
部分:
<DataGrid Name="dgLogView" ItemsSource="{Binding}" />
And here is the code behind: 这是后面的代码:
using (XmlLogfileStream logfileStream = new XmlLogfileStream(filename))
{
// File contents to read
// <LogInfo><Time>2015-03-14 17:01:43</Time><Message>Logging first time with XML in C#</Message></LogInfo>
// <LogInfo><Time>2015-03-14 17:02:11</Time><Message>Logging first time with XML in C#</Message></LogInfo>
// ...
DataSet ds = new DataSet();
ds.ReadXml(logfileStream);
dgLogView.ItemsSource = ds.Tables["LogInfo"].AsEnumerable();
}
A screenshot of the issue: 问题的屏幕截图:
I got the solution by changing my code as follows: 我通过更改代码来获得解决方案,如下所示:
using (XmlLogfileStream logfileStream = new XmlLogfileStream(filename))
{
DataSet ds = new DataSet();
DataTable dataTable = new DataTable("LogInfo");
dataTable.Columns.Add("Time", typeof(string));
dataTable.Columns.Add("Message", typeof(string));
ds.Tables.Add(dataTable);
ds.ReadXml(logfileStream);
dgLogView.ItemsSource = dataTable.DefaultView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.