需求:完成一个城市和区号的xml配置文件
处理思路:通过HtmlAgilityPack解析一个区号页面,生产xml文件
页面:http://www.hljboli.gov.cn/html/code.html
代码:
public void LoadUrl(string url)
{ try
{
//Tab1
this.webBrowser1.Url = new Uri(url); HtmlWeb web = new HtmlWeb();
//不加这行中文会乱码
web.OverrideEncoding = Encoding.Default;
HtmlAgilityPack.HtmlDocument doc = web.Load(url);
//通过反射获取 internal 字段值
FieldInfo info = doc.GetType().GetField("Text", BindingFlags.Instance | BindingFlags.NonPublic);
var text = info.GetValue(doc).ToString();
//Tab2
this.richTextBox1.Text = text; var sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>").AppendLine();
sb.Append("<codes>").AppendLine(); //xpath表达式
var provinceNodes = doc.DocumentNode.SelectNodes("//tr"); //过滤重复的区号
var tempList = new List<string>();
foreach (var provinceNode in provinceNodes)
{
//xpath表达式
var cityNodes = provinceNode.SelectNodes("td"); if (cityNodes.Count == )
{
if (cityNodes[].InnerText != " ") //去除空白行
sb.AppendFormat("<!--{0}-->", cityNodes[].InnerText).AppendLine();
}
if (cityNodes.Count == )
{
if (cityNodes[].InnerText != "长途区号")
{
if (!tempList.Contains(cityNodes[].InnerText))
{
sb.AppendFormat("<code name=\"{0}\" value=\"{1}\" />", cityNodes[].InnerText, cityNodes[].InnerText).AppendLine();
tempList.Add(cityNodes[].InnerText);
} if (cityNodes[].InnerText != " " && !tempList.Contains(cityNodes[].InnerText))//去除行不满情况
{
sb.AppendFormat("<code name=\"{0}\" value=\"{1}\" />", cityNodes[].InnerText, cityNodes[].InnerText).AppendLine();
tempList.Add(cityNodes[].InnerText);
}
}
}
}
sb.Append("</codes>").AppendLine(); this.richTextBox2.Text = sb.ToString(); }
catch (Exception)
{ } }
注意:
1.中文乱码,需要设置 web.OverrideEncoding = Encoding.Default;
2.通过反射访问HtmlDocument的内置字段Text获取body的内容
FieldInfo info = doc.GetType().GetField("Text", BindingFlags.Instance | BindingFlags.NonPublic);
var text = info.GetValue(doc).ToString();
3.通过xpath表达式获取节点元素