private void toolStripButton1_Click(object sender, EventArgs e)
{
XmlNode firstXmlNode = xmlDoc.SelectSingleNode("Employees");
foreach (XmlNode childNode in firstXmlNode.ChildNodes)
{
if (childNode != null)
{
TreeNode treeNode = new TreeNode(childNode.Attributes["name"].Value.ToString());
treeNode.Tag = childNode;
foreach (XmlNode xmlNode in childNode.ChildNodes)
{
if (xmlNode != null)
{
TreeNode node = new TreeNode(xmlNode.ChildNodes[1].InnerText);
node.Tag = xmlNode;
treeNode.Nodes.Add(node);
}
}
treeView1.Nodes.Add(treeNode);
}
}
------解决方案--------------------------------------------------------
XmlNode firstXmlNode = xmlDoc.SelectSingleNode("descendant::Employees");
------解决方案--------------------------------------------------------
if(firstXmlNode != null)
{
foreach (XmlNode childNode in firstXmlNode.ChildNodes)
{
....
}
------解决方案--------------------------------------------------------
debug时查看一下xmlDoc有没有Employees节点
------解决方案--------------------------------------------------------
XmlNode firstXmlNode = xmlDoc.SelectSingleNode("Employees");
取到的firstXMLNode应该为null Employees 看看是否有相关节点,是否有拼写错误