使用wpf做了窗体,跟Silverlight开发环境一样,将前台设计与后台开发逻辑分离开来,抓取南京市九个PM 2.5监测站点的数据
前台代码:
<Grid><Button Content="数据获取" Height="23" HorizontalAlignment="Left" Margin="487,23,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /><DataGrid AutoGenerateColumns="False" Height="338" HorizontalAlignment="Left" Margin="0,57,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="591" CanUserAddRows="False"><DataGrid.Columns><DataGridTextColumn Header="名称" IsReadOnly="True" Binding="{Binding Name,Mode=TwoWay}" Width="200" /> <DataGridTextColumn Header="PM2.5浓度" IsReadOnly="True" Binding="{Binding Density,Mode=TwoWay}" Width="200"/></DataGrid.Columns></DataGrid><Label Content="网址:" Height="28" HorizontalAlignment="Left" Margin="29,23,0,0" Name="label1" VerticalAlignment="Top" /><TextBox Height="23" HorizontalAlignment="Left" Margin="88,25,0,0" Name="textBox1" VerticalAlignment="Top" Width="362" /></Grid>
后台代码:
class MonitorInfo{public string Name{get;set;}public string Density{get;set;}};List<MonitorInfo> myListString=new List<MonitorInfo>();private string GetWebContent(string Url){string strResult = "";try{HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);//声明一个HttpWebRequest请求 request.Timeout = 30000;//设置连接超时时间 request.Headers.Set("Pragma", "no-cache");HttpWebResponse response = (HttpWebResponse)request.GetResponse();Stream streamReceive = response.GetResponseStream();Encoding encoding = Encoding.GetEncoding("GB2312");StreamReader streamReader = new StreamReader(streamReceive, encoding);strResult = streamReader.ReadToEnd();}catch{System.Windows.Forms.MessageBox.Show("出错");}return strResult;} private void button1_Click(object sender, RoutedEventArgs e){textBox1.Text="http://www.pm2d5.com/city/nanjing.html";String Url = textBox1.Text;string strWebContent = GetWebContent(Url);int divIndex = strWebContent.IndexOf("weilai");int tableStartIndex = strWebContent.IndexOf("<table", divIndex);int tableEndIndex = strWebContent.IndexOf("</table>", tableStartIndex);string strWeb = strWebContent.Substring(tableStartIndex, tableEndIndex - tableStartIndex + 8);//生成HtmlDocumentWebBrowser web = new WebBrowser();web.Navigate("about:blank");HtmlDocument htmldoc = web.Document.OpenNew(true);htmldoc.Write(strWeb);HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("tr");int i = 0;myListString.Clear();foreach (HtmlElement tr in htmlTR){if (i == 0){i++;continue;//跳过表头}MonitorInfo monitorInfo = new MonitorInfo();monitorInfo.Name = tr.GetElementsByTagName("td")[0].InnerText.ToString();monitorInfo.Density = tr.GetElementsByTagName("td")[3].InnerText.ToString();myListString.Add(monitorInfo);}dataGrid1.ItemsSource = myListString;}}
在MonitorInfo这个类的设计中,注意使用get,set,否则将List数据绑定到DataGrid后会出现空白行