0
我正在處理一個代碼,其中我正在遍歷包含特定文件夾結構的XML文件。我想以表格格式顯示文件夾名稱和文件。以下是我的代碼:無法在Datagridview中查看錶的所有行
DataTable dtCheckPropertyOutput = new DataTable();
dtCheckPropertyOutput.Columns.Add("Property Name");
dtCheckPropertyOutput.Columns.Add("Expected value");
string folderName = null;
string fileName = null;
// display each folder
foreach (var folder in mainFolder.Elements())
{
DataRow anyRow = dtCheckPropertyOutput.NewRow();
folderName = folder.Attribute("Name").Value;
MessageBox.Show(folderName);
anyRow["Property Name"] = "Folder";
anyRow["Expected Value"] = folderName;
// display each file
foreach (var file in folder.Elements())
{
fileName = file.Attribute("Name").Value;
anyRow["Property Name"] = "File";
anyRow["Expected Value"] = fileName;
}
dtCheckPropertyOutput.Rows.Add(anyRow);
dataGridView1.DataSource = dtCheckPropertyOutput;
問題是我只能查看xml中最後一個元素,但不能查看所有條目。對於例如我可以查看文件3和文件5只[而不是文件夾1,文件1,文件夾2和文件2]如果XML是如下:
<Root>
<MainFolder Name="Main Folder">
<Folder Name="Folder1">
<File Name="File1" />
<File Name="File3" />
</Folder>
<Folder Name="Folder2">
<File Name="File2" />
<File Name="File5" />
</Folder>
</MainFolder>
</Root>
請,有人幫助我。提前致謝。