如何为WP7输出HTML敏捷包
本文关键字:HTML 输出 WP7 | 更新日期: 2023-09-27 18:07:23
我有下面的代码来解析一个html页面的一部分。
我想知道的是如何将其输出到列表框或文本框。
每当我尝试,我得到未处理的异常错误
private void PhoneApplicationPage_Loaded(对象发送者,RoutedEventArgs e){
var doc = new HtmlDocument();
doc.LoadHtml("http://www.sourceURL.com");
var node = doc.DocumentNode.Descendants("div").FirstOrDefault(x => x.Id == "FlightInfo_FlightInfoUpdatePanel");
var value = node.InnerHtml;
this.textBox1.Text = value;
异常错误是:
System.NullReferenceException was unhandled
Message: NullReferenceException
堆栈跟踪为:
at Auckland_Airport.MainPage.PhoneApplicationPage_Loaded(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
HTML布局
<div id="FlightInfo_FlightInfoUpdatePanel">
<table cellspacing="0" cellpadding="0"><tbody>
<tr class=""><td class="airline"><img src="/images/airline logos/US.gif" title="US AIRWAYS. " alt="US AIRWAYS. " /></td><td class="flight">US5316</td><td class="codeshare">NZ46</td><td class="origin">Rarotonga</td><td class="date">02 Sep</td><td class="time">10:30</td><td class="est">21:30</td><td class="status">CHECK IN CLOSING</td></tr><tr class="alt"><td class="airline"><img src="/images/airline logos/NZ.gif" title="AIR NEW ZEALAND LIMITED. " alt="AIR NEW ZEALAND LIMITED. " /></td><td class="flight">NZ6</td><td class="codeshare"> </td><td class="origin">Los Angeles</td><td class="date">02 Sep</td><td class="time">19:15</td><td class="est">19:15</td><td class="status">DEPARTED</td></tr><tr class=""><td class="airline"><img src="/images/airline logos/AC.gif" title="Air Canada. " alt="Air Canada. " /></td><td class="flight">AC6093</td><td class="codeshare">NZ6</td><td class="origin">Los Angeles</td><td class="date">02 Sep</td><td class="time">19:15</td><td class="est">19:15</td><td class="status">DEPARTED</td></tr><tr class="alt"><td class="airline"><img src="/images/airline class="d
</div>
</div>
HTML 清晰
<div id="FlightInfo_FlightInfoUpdatePanel">
<table cellspacing="0" cellpadding="0"><tbody>
<tr class="">
<td class="airline"><img src="/images/airline logos/NZ.gif" title="AIR NEW ZEALAND LIMITED. " alt="AIR NEW ZEALAND LIMITED. " /></td>
<td class="flight">NZ8</td>
<td class="codeshare"> </td>
<td class="origin">San Francisco</td>
<td class="date">01 Sep</td>
<td class="time">17:15</td>
<td class="est">18:00</td>
<td class="status">DEPARTED</td>
</tr>
<tr class="alt">
<td class="airline"><img src="/images/airline logos/AC.gif" title="Air Canada. " alt="Air Canada. " /></td>
<td class="flight">AC6103</td>
<td class="codeshare">NZ8</td>
<td class="origin">San Francisco</td>
<td class="date">01 Sep</td>
<td class="time">17:15</td>
<td class="est">18:00</td>
HtmlDocument。Load函数不接受Uri作为参数,它应该接收一个结构化的HTML,该HTML是以某种方式从服务器检索到的。
以下是使用WebClient类从服务器检索HTML的示例代码: WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(@"http://www.aucklandairport.co.nz/en/FlightInformation/DomesticArrivalsAndDepartures.aspx"));
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var html = e.Result;
var doc = new HtmlDocument();
doc.LoadHtml(html);
var list = doc.DocumentNode.Descendants("div").ToList();
var node = doc.DocumentNode.Descendants("div").FirstOrDefault(x => x.Id == "FlightInfo_FlightInfoUpdatePanel");
var value = string.Empty;
if (node != null)
value = node.InnerHtml;
}
最后,value
变量保存了需要进一步解析的内部HTML。