У меня есть таблица данных со следующими столбцами. < /p> [code] dataTable.Columns.Add("ID", typeof(string)); dataTable.Columns.Add("File Name", typeof(string)); dataTable.Columns.Add("Date Taken", typeof(string)); dataTable.Columns.Add("Size", typeof(string)); dataTable.Columns.Add("Unique", typeof(bool)); < /code> Таблица данных записывается в файл XML следующим образом < /p> if (dgView.DataSource is DataTable dataTable) // Ensure DataSource is a DataTable { if (string.IsNullOrWhiteSpace(dataTable.TableName)) { dataTable.TableName = "MyDataTable"; // Assign a name to avoid serialization error }
< /code> Когда я читаю файл следующим образом, данные не читаются. (Количество строк равно 0.) < /p> private void btnRetrieve_Click(object sender, EventArgs e) { OpenFileDialog OpenFileDialog = new OpenFileDialog(); if (OpenFileDialog.ShowDialog() == DialogResult.OK) { string fileName = OpenFileDialog.FileName; Task.Run(() => { // Load the data from XML into the DataTable DataTable dataTable = new DataTable(); dataTable.Columns.Add("ID", typeof(string)); dataTable.Columns.Add("File_x0020_Name", typeof(string)); // Handle space as _x0020_ dataTable.Columns.Add("Date_x0020_Taken", typeof(string)); // Handle space as _x0020_ dataTable.Columns.Add("Size", typeof(string)); dataTable.Columns.Add("Unique", typeof(bool));
// Read the XML into the DataTable if (File.Exists(fileName)) { try { dataTable.ReadXml(fileName);
// Check if data has been loaded if (dataTable.Rows.Count == 0) { this.Invoke(new Action(() => { MessageBox.Show("The XML file was loaded, but no data was found.", "Data Load Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); })); } else { // Populate the dictionary with data from the DataTable ConcurrentDictionary g_allFiles = new ConcurrentDictionary();
foreach (DataRow row in dataTable.Rows) { mediaFile media = new mediaFile { id = row["ID"].ToString(), fileName = row["File_x0020_Name"].ToString(), exif_date = row["Date_x0020_Taken"].ToString(), fileSize = row["Size"].ToString(), unique = Convert.ToBoolean(row["Unique"]) };
// Update DataGridView with loaded data this.Invoke(new Action(() => { dgView.DataSource = dataTable; })); } } catch (Exception ex) { // Handle errors in reading the XML this.Invoke(new Action(() => { MessageBox.Show($"Error reading the XML file: {ex.Message}", "File Read Error", MessageBoxButtons.OK, MessageBoxIcon.Error); })); } } else { // If file not found, show a message on the UI thread this.Invoke(new Action(() => { MessageBox.Show("The specified file path does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); })); } }); } var k = g_allFiles; } [/code] Что я здесь делаю?