How come during the linq to xml parse my code is only reading one fo the xml files in the folder rather than all of them?
private void button1_Click(object sender, EventArgs e)
{
string[] fileEntries = Directory.GetFiles(@"", "*.cfg*");
foreach (string fileName in fileEntries)
{
XDocument doc = XDocument.Load(fileName);
var query = from x in doc.Descendants("")
select new
{
MaxChild = x.Element("Max").Value,
MinChild = x.Element("Min").Value
};
var query2 = from y in doc.Descendants("")
select new
{
MaxChild = y.Element("Max").Value,
MinChild = y.Element("Min").Value
};
var query3 = from z in doc.Descendants("")
select new
{
MaxChild = z.Element("Max").Value,
MinChild = z.Element("Min").Value
};
var bs3 = new BindingSource { DataSource = query.Union(query2.Union(query3)) };
dataGridView1.AutoGenerateColumns = true;
dataGridView1.AutoSize = true;
dataGridView1.DataSource = bs3;
dataGridView1.Columns[0].HeaderText = "Max";
dataGridView1.Columns[1].HeaderText = "Min";
}
}
You’re iterating over the files in the directory – but simply reassigning the datasource each time. In other words, it’s exactly the same problem as in your earlier question, except that instead of it being to do with three queries, it’s to do with reassigning it for each file.
Try this instead, using a single query:
EDIT: Okay, if you know there’s going to be exactly one of each element, it’s fairly easy: