一:xml文件
![](https://i-blog.csdnimg.cn/blog_migrate/19d19ae7e3e75f47538054d14baa66ce.png)
二:后台代码
private void ReadXml()
{
List<TreeViewItem> nodes = new List<TreeViewItem>();
XmlReader xmlReader = XmlReader.Create("Config.xml");
StringBuilder sbXml = new StringBuilder();
while (xmlReader.Read())
{
sbXml.Append("\n");
sbXml.Append("节点类型:" + xmlReader.NodeType.ToString());
switch (xmlReader.NodeType)
{
case XmlNodeType.DocumentType:
break;
case XmlNodeType.Element:
//得到节点的名称
string elementName = xmlReader.Name;//循环节点的属性
for (int i = 0; i < xmlReader.AttributeCount; i++)
{
xmlReader.MoveToAttribute(i);if (xmlReader.Value.Trim() != "" && xmlReader.Name.Trim() == "Title")
{
TreeViewItem node = new TreeViewItem();
node.Header = xmlReader.Value;
nodes.Add(node);
}
}
break;
case XmlNodeType.EndElement:
elementName = xmlReader.Name;
break;
default:
break;
}
}
treeView1.ItemsSource = nodes;
}
三:运行结果
![](https://i-blog.csdnimg.cn/blog_migrate/3ca13aacb5bcca657788e87c9b1575ad.png)