Friday, September 6, 2013

Read only one Record from the XML file and display it on listbox in WPF

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.
The updating should be dynamic. I tried binding but it did not help.
Here is my Xml file
<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
public class Collection
{
public List<Information> empList = new List<Information>();
}
This is the .cs file
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
<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"

No comments:

Post a Comment