NuGet里添加HtmlAgilityPack的引用
然后wp上使用必须添加本地
C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.Xml.XPath.dll
的引用
var doc = new HtmlDocument(); doc.Load(……);
之后便可以像操作XML一样操作HTML了,非常方便
http://htmlagilitypack.codeplex.com/
比如:
private async Task<ObservableCollection<ACItem>> getList(ObservableCollection<ACItem> list, string url) { var request = (HttpWebRequest)WebRequest.Create(new Uri(url)); request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"; var response = await request.GetResponseAsync(); var doc = new HtmlDocument(); doc.Load(response.GetResponseStream()); …………………… return list; }
参见:http://blog.liubaicai.com/?p=195
另外wp中使用async和await访问网络的方法:
http://blog.liubaicai.com/?p=192
使用async和await可以大量减少代码量,美化代码,更清晰(封装了异步的方法)