using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace oop内测_XMl加载
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
//加载
public Dictionary<string, Students> dic = new Dictionary<string, Students>();
//加载xml文档
public void LoadXMLInfo()
{
this.dic.Clear();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("School.xml");
XmlNode xmlNode = xmldoc.DocumentElement;
foreach (XmlNode xmlOne in xmlNode.ChildNodes)
{
Students stu = new Students();
stu.Id = xmlOne.Attributes["id"].Value;
stu.Name = xmlOne.SelectSingleNode("name").InnerText;
stu.Age = int.Parse(xmlOne.SelectSingleNode("age").InnerText);
stu.Sex = xmlOne.SelectSingleNode("sex").InnerText;
stu.Fans =int.Parse( xmlOne.SelectSingleNode("fans").InnerText);
stu.Url = xmlOne["blog"].Attributes["url"].Value;
this.dic.Add(stu.Id, stu);
}
}
//加载树控件
public void TreeLoadInfo()
{
this.tvNodes.Nodes.Clear();
TreeNode trOne = null;
foreach (string item in this.dic.Keys)
{
trOne = new TreeNode(item);
TreeNode tr2 = new TreeNode(dic[item].Name);
TreeNode tr3 = new TreeNode(dic[item].Age.ToString());
TreeNode tr4 = new TreeNode(dic[item].Sex);
TreeNode tr5 = new TreeNode(dic[item].Fans.ToString());
TreeNode tr6 = new TreeNode(dic[item].Url);
this.tvNodes.Nodes.Add(trOne);
trOne.Nodes.Add(tr2);
trOne.Nodes.Add(tr3);
trOne.Nodes.Add(tr4);
trOne.Nodes.Add(tr5);
trOne.Nodes.Add(tr6);
}
}
private void button1_Click(object sender, EventArgs e)
{
LoadXMLInfo();
TreeLoadInfo();
}
}
}
students类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace oop内测_XMl加载
{
public class Students
{
private string id;
public string Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string url;
public string Url
{
get { return url; }
set { url = value; }
}
private int fans;
public int Fans
{
get { return fans; }
set { fans = value; }
}
}
}
xml文档数据
<?xml version="1.0" encoding="utf-8" ?>
<students>
<student id="001">
<name>王菲菲</name>
<age>20</age>
<sex>女</sex>
<fans>159999</fans>
<blog url="http://www.baidu.com"/>
</student>
<student id="002">
<name>周杰伦</name>
<age>26</age>
<sex>男</sex>
<fans>15159999</fans>
<blog url="http://www.tencten.com"/>
</student>
<student id="003">
<name>小李子</name>
<age>26</age>
<sex>男</sex>
<fans>9159999</fans>
<blog url="http://www.xilizi.com"/>
</student>
<student id="004">
<name>王豪</name>
<age>24</age>
<sex>男</sex>
<fans>39999</fans>
<blog url="http://www.souhu.com"/>
</student>
</students>