C#实现中国行政区划数据下载
private string BaseURL = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/"; public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// 从网页的Url链接加载
var url = BaseURL + "index.html";
var web = new HtmlWeb();
web.OverrideEncoding = Encoding.GetEncoding("gb2312");
var doc = web.Load(url, "get");
HtmlNodeCollection provinces = doc.DocumentNode.SelectNodes("//tr[@class="provincetr"]/td");
foreach (HtmlNode province in provinces)
{
TreeNode node = new TreeNode();
node.Tag = province.InnerText.Trim();
node.Name = province.FirstChild.Attributes["href"].Value.Trim().Split(".")[0];
node.Text = "[" + node.Name + "]" + node.Tag;
areaTreeView.Nodes["0"].Nodes.Add(node);
}
areaTreeView.Nodes["0"].Expand();
}
private void areaTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
loadChild(this.areaTreeView.SelectedNode);
}
/// <summary>
/// 加载地市
/// </summary>
/// <param name="parentCode"></param>
private void loadCity(TreeNode root, string parentCode)
{
//citytr
var url = BaseURL + parentCode + ".html";
var web = new HtmlWeb();
web.OverrideEncoding = Encoding.GetEncoding("gb2312");
var doc = web.Load(url, "get");
HtmlNodeCollection citys = doc.DocumentNode.SelectNodes("//tr[@class="citytr"]");
if (citys != null)
{
foreach (HtmlNode city in citys)
{
TreeNode node = new TreeNode();
HtmlNode code = city.FirstChild;
node.Tag = code.NextSibling.InnerText.Trim();
node.Name = code.InnerText.Trim().Substring(0,4);
node.Text = "[" + node.Name + "]" + node.Tag;
root.Nodes.Add(node);
}
root.Expand();
}
}
/// <summary>
/// 加载区县
/// </summary>
/// <param name="parentCode"></param>
private void loadCounty(TreeNode root, string parentCode)
{
//countytr
var url = BaseURL + parentCode.Substring(0, 2) + "/" + parentCode + ".html";
var web = new HtmlWeb();
web.OverrideEncoding = Encoding.GetEncoding("gb2312");
var doc = web.Load(url, "get");
HtmlNodeCollection citys = doc.DocumentNode.SelectNodes("//tr[@class="countytr"]");
if (citys != null)
{
foreach (HtmlNode city in citys)
{
TreeNode node = new TreeNode();
HtmlNode code = city.FirstChild;
node.Tag = code.NextSibling.InnerText.Trim();
node.Name = code.InnerText.Trim().Substring(0,6);
node.Text = "[" + node.Name + "]" + node.Tag;
root.Nodes.Add(node);
}
root.Expand();
}
}
/// <summary>
/// 加载乡镇
/// </summary>
/// <param name="parentCode"></param>
private void loadTown(TreeNode root,string parentCode)
{
//towntr
var url = BaseURL + parentCode.Substring(0, 2) + "/" + parentCode.Substring(2, 2) + "/" + parentCode + ".html";
var web = new HtmlWeb();
web.OverrideEncoding = Encoding.GetEncoding("gb2312");
var doc = web.Load(url, "get");
HtmlNodeCollection citys = doc.DocumentNode.SelectNodes("//tr[@class="towntr"]");
if (citys != null)
{
foreach (HtmlNode city in citys)
{
TreeNode node = new TreeNode();
HtmlNode code = city.FirstChild;
node.Tag = code.NextSibling.InnerText.Trim();
node.Name = code.InnerText.Trim().Substring(0, 9);
node.Text = "[" + node.Name + "]" + node.Tag;
root.Nodes.Add(node);
}
root.Expand();
}
}
private void loadChild(TreeNode root)
{
string parentCode = root.Name;
if (root.Level == 1)
{
this.loadCity(root,parentCode);
}
else if (root.Level == 2)
{
this.loadCounty(root, parentCode);
}
else if (root.Level == 3)
{
this.loadTown(root, parentCode);
}
}
private void btnDownloadSelect_Click(object sender, EventArgs e)
{
TreeNode root = this.areaTreeView.SelectedNode;
if (root == null)
{
MessageBox.Show("请选择节点", "提示", MessageBoxButtons.OK);
return;
}
loadAllChild(root);
string result1 = root.Text+@".txt";//结果保存到桌面
FileStream fs = new FileStream(result1, FileMode.Append);
StreamWriter wr = null;
wr = new StreamWriter(fs);
exportNode(root, wr);
wr.Close();
}
private void loadAllChild(TreeNode root)
{
if (root.Nodes.Count <= 0)
{
loadChild(root);
}
foreach (TreeNode node in root.Nodes)
{
loadAllChild(node);
}
}
private void exportNode(TreeNode root, StreamWriter wr)
{
wr.WriteLine(root.Name + " " + root.Tag);
foreach (TreeNode node in root.Nodes)
{
exportNode(node,wr);
}
}
以上是 C#实现中国行政区划数据下载 的全部内容, 来源链接: utcz.com/z/511719.html