xml文件的讀寫可以參考我以前的博客
【xm基礎知識】
【基於DOM的XML文檔的讀,寫,修改代碼實例】
【XML文件的讀寫-------基於DOM的XML文件操作】
xml文件可以在我的資源中的下載,點擊這裏
代碼示例:
private void button7_Click(object sender, EventArgs e)
{
string xmlpath = Application.StartupPath + "//DatabaseParameters.xml";
if(File.Exists(xmlpath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlpath);
XmlElement xmlElement = xmlDoc.DocumentElement;
XmlNodeList xmlNodeList = xmlElement.ChildNodes;//獲取子節點集合
List<string> statistics =new List <string>();
for (int i=0;i< xmlNodeList.Count;i++)//得到每個數據庫中有幾張表
{
XmlNode node = xmlNodeList[i];
XmlNodeList xmlNodeList2 = node.ChildNodes;//node元素的子節點列表
statistics.Add(xmlNodeList[i].Attributes["name"].Value);//數據庫的名字
TreeNode RootNode = new TreeNode(xmlNodeList[i].Attributes["name"].Value);//父節點
treeView1.Nodes.Add(RootNode);
for (int j=0;j< xmlNodeList2.Count;j++)
{
statistics.Add(xmlNodeList2[j].Attributes["name"].Value);//表的名字
RootNode.Nodes.Add(xmlNodeList2[j].Attributes["name"].Value);//子節點
}
foreach(TreeNode thf in RootNode.Nodes)
{
thf.ImageIndex = 1;
thf.SelectedImageIndex = 1;
}
}
}
else
{
MessageBox.Show("文件不存在!");
}
}
效果如下: