[英]Read only one Record from the XML file and display it on listbox in WPF
I have an Xml file and it has some Elements, I want to display only one on the Listbox and the listbox should be updated when new Records are added. 我有一个Xml文件,它有一些元素,我只想在列表框上显示一个,添加新记录时应该更新列表框。 The updating should be dynamic.
更新应该是动态的。 I tried binding but it did not help.
我尝试绑定,但没有帮助。
Here is my Xml file 这是我的Xml文件
<empList>
<Information>
<Name>Jack</Name>
<Destination>AA</Destination>
<EmployeeID>AA</EmployeeID>
</Information>
<Information>
<Name>David</Name>
<Destination>BB</Destination>
<EmployeeID>BB</EmployeeID>
</Information>
<Information>
<Name>Adam</Name>
<Destination>wdwad</Destination>
<EmployeeID>dwad</EmployeeID>
</Information></empList>
This is the class file 这是课程文件
public class Information
{
public string Name{ get; set; }
public string Destination{ get; set; }
public string EmployeeID{ get; set; }
}
This is the Collection class file 这是Collection类文件
public class Collection
{
public List<Information> empList = new List<Information>();
}
This is the .cs file 这是.cs文件
private void Window_Loaded(object sender, RoutedEventArgs e)
{
XmlSerializer xs = new XmlSerializer(typeof(Collection));
FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Collection coll = (Collection)xs.Deserialize(read);
listBox1.ItemsSource = coll.empList;
}
This is the XAML file 这是XAML文件
<ListBox Height="251" HorizontalAlignment="Left" Margin="334,22,0,0" Name="listBox1" VerticalAlignment="Top" Width="170"
DataContext="{Binding {StaticResource Data}, XPath=empList/Information}"
ItemsSource="{Binding XPath=Information/@Name}" />
Now i want to display only name on the listbox and the listbox should be automatically updated when new records are added. 现在,我只想在列表框中显示名称,并且在添加新记录时应该自动更新列表框。 When i execute the abov mentioned code, i get an exception in the xaml file like "Provide value on 'System.Windows.StaticResourceExtension"
当我执行上面提到的代码时,我在xaml文件中得到一个异常,例如“在'System.Windows.StaticResourceExtension上提供值”
You can bind property in WPF and XML file is collection of Information so you need to add Collection tag at start Try This: 您可以在WPF中绑定属性,而XML文件是信息的集合,因此您需要在开始时添加Collection标签。
XAML: XAML:
<Grid >
<ListBox Height="251" HorizontalAlignment="Left" Margin="334,22,0,0" Name="listBox1" VerticalAlignment="Top" Width="170"
DisplayMemberPath="Name" />
</Grid>
Collection: 采集:
public class Collection
{
public ObservableCollection<Information> empList { get; set; }
public Collection()
{
empList = new ObservableCollection<Information>();
}
}
XML Deserialize: XML反序列化:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
XmlSerializer xs = new XmlSerializer(typeof(Collection));
FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Collection coll = (Collection)xs.Deserialize(read);
listBox1.ItemsSource = coll.empList;
}
XML file: XML档案:
<?xml version="1.0" encoding="utf-8" ?>
<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<empList>
<Information>
<Name>Jack</Name>
<Destination>AA</Destination>
<EmployeeID>AA</EmployeeID>
</Information>
<Information>
<Name>David</Name>
<Destination>BB</Destination>
<EmployeeID>BB</EmployeeID>
</Information>
<Information>
<Name>Adam</Name>
<Destination>wdwad</Destination>
<EmployeeID>dwad</EmployeeID>
</Information>
</empList>
</Collection>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.