我正在尝试将 xml 文件导入 ac# 数据网格视图。但是每当我使用 .SelectNodes 时,都不会将任何标签读入我的 xmlnodelist。每当我对代码运行调试并在它到达第一个 foreach 语句时单步执行代码时,它会立即离开该语句,我假设是因为 xmlnodelist 中没有任何内容。这是我到目前为止。请帮忙。提前致谢
namespace DesktopApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
int currRow = 0,currCell =0;
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string fileType = ofd.FileName;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(fileType);
XmlNode root = xDoc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("df", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("i", "http://www.w3.org/2001/XMLSchema-instance");
XmlNodeList listOfEmployees = root.SelectNodes("descendant::Employee",nsmgr);
foreach (XmlNode employee in listOfEmployees)
{
foreach (XmlNode characteristic in employee)
{
if (characteristic.InnerText != null)
{
dataGridView1.Rows[currRow].Cells[currCell].Value = characteristic.InnerText;
}
currRow++;
currCell++;
}
}
}
}
}
相关分类