【发布时间】:2011-09-01 08:45:07
【问题描述】:
我目前正在尝试从 HTML 文档中的 DIV 解析特定表。
我有这个工作 Windows Silverlight,但 WP7 HTML 敏捷包似乎完全不同。
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>
我目前正在使用此代码来解析“FlightInfo_FlightInfoUpdatePanel”DIV 框中的表格
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using HtmlAgilityPack;
namespace Auckland_Airport
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
var doc = new HtmlDocument();
doc.LoadHtml("http://www.SourceURL");
var flightTableCell = doc.DocumentNode.Descendants("div")
.FirstOrDefault(x => x.ID = "FlightInfo_FlightInfoUpdatePanel")
.Element("table")
.Element("tbody")
.Elements("td")
.FirstOrDefault(x => x.Attributes.Contains("flight"));
var value = flightTableCell.InnerText;
这会产生一个未处理的异常错误:
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)
【问题讨论】:
-
您的实际问题是什么?如何将 IEnumerable 绑定到 ListBox ?
-
文本框还是列表框?你能决定吗?你当前的代码有什么问题,你在哪里获取内容?
-
@claus Jorgensen 或者,我只需要知道如何从 HTML 和输出中解析。我需要知道如何将 id 转换为字符串。抱歉,上面的代码可能完全错误,wp7 的 HTML Agilty 包与标准包非常不同。
-
所以你甚至没有尝试过你自己的代码?
-
@claus jorgensen 我一直找不到如何使用这个敏捷包的示例。这就是我寻求帮助的原因。我花了几个小时尝试上述方法的变体,但无法解决。如果您有任何建议,请提供。如果不是请不要评论。
标签: c# xml linq windows-phone-7 html-agility-pack