我试了一下,没有任何问题啊.下面是我复制你的代码;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;namespace Tester
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(0, 48);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(288, 120);
this.treeView1.TabIndex = 0;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 249);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1});
this.statusBar1.Size = new System.Drawing.Size(292, 24);
this.statusBar1.TabIndex = 1;
this.statusBar1.Text = "statusBar1";
//
// statusBarPanel1
//
this.statusBarPanel1.Text = "statusBarPanel1";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar1,
this.treeView1});
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
PopulateTreeView();
}
protected void PopulateTreeView()
{
statusBarPanel1.Text="Refreshing Employee Codes. Please wait...";
this.Cursor = Cursors.WaitCursor;
treeView1.Nodes.Clear();
TreeNode tvRootNode=new TreeNode("Employee Records");
this.Cursor = Cursors.Default;
treeView1.Nodes.Add(tvRootNode); TreeNodeCollection nodeCollect = tvRootNode.Nodes;
string strVal="";
XmlTextReader reader= new XmlTextReader(@"c:\xml.xml");
//reader.MoveToElement();
try
{
while(reader.Read())
{
if(reader.HasAttributes && reader.NodeType==XmlNodeType.Element)
{
reader.MoveToElement();
//reader.MoveToElement();
reader.MoveToAttribute("Id");
strVal=reader.Value;
reader.Read();
reader.Read();
if(reader.Name=="Dept")
{
reader.Read();
} //create the child nodes
TreeNode EcodeNode = new TreeNode(strVal);
// Add the Node
nodeCollect.Add(EcodeNode);
}
}
statusBarPanel1.Text="Click on an employee code to see their record.";
}
catch(XmlException e)
{
MessageBox.Show("XML Exception :"+e.ToString());
}
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form2());
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;namespace Tester
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(0, 48);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(288, 120);
this.treeView1.TabIndex = 0;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 249);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1});
this.statusBar1.Size = new System.Drawing.Size(292, 24);
this.statusBar1.TabIndex = 1;
this.statusBar1.Text = "statusBar1";
//
// statusBarPanel1
//
this.statusBarPanel1.Text = "statusBarPanel1";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar1,
this.treeView1});
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
PopulateTreeView();
}
protected void PopulateTreeView()
{
statusBarPanel1.Text="Refreshing Employee Codes. Please wait...";
this.Cursor = Cursors.WaitCursor;
treeView1.Nodes.Clear();
TreeNode tvRootNode=new TreeNode("Employee Records");
this.Cursor = Cursors.Default;
treeView1.Nodes.Add(tvRootNode); TreeNodeCollection nodeCollect = tvRootNode.Nodes;
string strVal="";
XmlTextReader reader= new XmlTextReader(@"c:\xml.xml");
//reader.MoveToElement();
try
{
while(reader.Read())
{
if(reader.HasAttributes && reader.NodeType==XmlNodeType.Element)
{
reader.MoveToElement();
//reader.MoveToElement();
reader.MoveToAttribute("Id");
strVal=reader.Value;
reader.Read();
reader.Read();
if(reader.Name=="Dept")
{
reader.Read();
} //create the child nodes
TreeNode EcodeNode = new TreeNode(strVal);
// Add the Node
nodeCollect.Add(EcodeNode);
}
}
statusBarPanel1.Text="Click on an employee code to see their record.";
}
catch(XmlException e)
{
MessageBox.Show("XML Exception :"+e.ToString());
}
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form2());
}
}
}
{
private XmlDocument doc;
private string xmlFileName; public XmlConfig(string filename)
{
xmlFileName = filename;
doc = new XmlDocument();
try
{
doc.Load(xmlFileName);
}
catch
{
//doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
}
} public void Save()
{
try
{
doc.Save(xmlFileName);
}
catch
{
}
} /// <summary>
/// 读XML节点
/// </summary>
/// <param name="key">节点KEY</param>
/// <param name="value">默认值</param>
/// <returns>如节点存在,返回值,否则,返回默认值</returns>
public string Read(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
return node.InnerText;
else
return value;
} /// <summary>
/// 读XML子节点
/// </summary>
/// <param name="key">父节点值</param>
/// <param name="Childkey">子节点值</param>
/// <param name="value">默认值</param>
/// <returns>如节点存在,返回值,否则,返回默认值</returns>
public string Read(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if(node!=null)
{
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
return node2.InnerText;
}
else
return value;
}
else
return value;
}
public XmlNodeList NodeRead()
{
XmlNode node = doc.ChildNodes[1];
XmlNodeList nodelist = node.ChildNodes;
return nodelist;
}
}