用XML给treeview 添加数据
<?xml version="1.0" encoding="utf-8" ?>
<Student>
<Name>张靓靓</Name>
<Age>20</Age>
<Hobby>唱歌歌</Hobby>
<Name>周杰杰</Name>
<Age>22</Age>
<Hobby>耍双节棍棍</Hobby>
</Student>
以上是 XML文件内容 显示效果如下:
+张靓靓
20
唱歌歌
+周杰杰
22
耍双节棍棍
[解决办法]
System.IO.FileStream fs = System.IO.File.OpenRead(Application.StartupPath + "\\XMLFile1.xml");
System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load(fs);
fs.Close();
TreeNode node = null;
foreach (System.Xml.Linq.XElement el in doc.Root.Elements())
{
if (string.Compare(el.Name.LocalName, "Name", true) == 0)
{
node = treeView1.Nodes.Add(el.Value);
}
else
{
node.Nodes.Add(el.Value);
}
}
System.IO.FileStream fs = System.IO.File.OpenRead(@"E:\第二学期\深入.NET平台和C#编程练习\第十章\HomeWork_07\HomeWork_07\Stu.xml");
System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load(fs);
fs.Close();
TreeNode node = null;
foreach (System.Xml.Linq.XElement el in doc.Root.Elements())
{
if (string.Compare(el.Name.LocalName, "Name", true) == 0)
{
node = tvStu.Nodes.Add(el.Value);
}
else
{
node.Nodes.Add(el.Value);
}
}