有人可以告诉我为什么这会引发空引用异常(对象引用未设置为对象的实例)。我的代码读取一个简单的 xml 文件,然后应该将 2 个元素中的文本传递给另一个方法。但是在 foreach 循环的第一行抛出异常:
请原谅我的无知,我是新人。:)
private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
{
//I have to pass 'this' (which is Form1) when creating the Projects (Form2) in order for them to understand / see each other
Projects myProjects = new Projects(this);
//displays the Form 2 (called Projects)
myProjects.Show();
XmlDocument xdoc = new XmlDocument();
xdoc.Load("O:\\TestDaws\\projects.xml");
string projList = "/Projects/Project";
XmlNodeList xprojects = xdoc.SelectNodes(projList);
foreach (XmlNode xNodeName in xprojects)
{
string projectname = xNodeName.SelectNodes("/ProjectName")[0].InnerText.ToString();
string projecttype = xNodeName.SelectNodes("/ProjectType")[0].InnerText.ToString();
myProjects.buildProjectList(projectname, projecttype);
}
}