|
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 shi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("city.xml");
XmlNode node = doc.DocumentElement;
TreeNode tree = null;
foreach (XmlNode node1 in node.ChildNodes)
{
if (node1.Name == "name")
{
tree = new TreeNode();
tree.Text = node1.InnerText;
treeView1.Nodes.Add(tree);
}
else
{
TreeNode tree1 = new TreeNode();
tree1.Text = node1.InnerText;
tree.Nodes.Add(tree1);
}
}
}
}
}
我的代码
<?xml version="1.0" encoding="utf-8"?>
<city>
<ID>01</ID>
<name>北京</name>
<subregions>
<ID>0101</ID>
<name>海淀区</name>
</subregions>
<subregions>
<ID>0102</ID>
<name>朝阳区</name>
</subregions>
<subregions>
<ID>0103</ID>
<name>西城区</name>
</subregions>
</city>
<city>
<ID>02</ID>
<name>辽宁</name>
<subregions>
<ID>0201</ID>
<name>沈阳市</name>
</subregions>
<subregions>
<ID>0202</ID>
<name>大连市</name>
</subregions>
<subregions>
<ID>0203</ID>
<name>鞍山市</name>
</subregions>
</city>
xml文件
该怎么读出来呢?为什么我总是报错
求详解,谢谢大神 |
|