Reputation: 341
I'm trying to load xml data in to my ListView control, but not working correctly.
The XML content is:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee ID="1">
<Name>Numeri</Name>
</Employee>
<Employee ID="5">
<Name>husu</Name>
</Employee>
<Employee ID="6">
<Name>sebri</Name>
</Employee>
</Employees>
This is what I have tried to load the data:
private void btn_load_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(Application.StartupPath + "/Employees.xml");
foreach (var dm in doc.Descendants("Employee"))
{
ListViewItem item = new ListViewItem(new string[]
{
dm.XAttribute("ID").Value,
dm.XElement("Name").Value
});
listView1.Items.Add(item);
}
It is preferable if the help is provided using XML-Linq.
Thanks in Advance.
Upvotes: 0
Views: 437
Reputation: 916
Could you try doing something as like,
XDocument doc = XDocument.Load(Application.StartupPath + "/Employees.xml");
doc.Descendants("Employee").ToList()
.ForEach(x => listView1.Items.Add(
new ListViewItem(
new string[] { x.Attribute("ID").Value, x.Element("Name").Value }))
);
Hope this helps...
Upvotes: 1