HTML Agilty for WP7 - Silverlight c#

本文关键字:Silverlight WP7 Agilty for HTML | 更新日期: 2023-09-27 18:07:17

我目前正在尝试从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">&nbsp;</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)

HTML Agilty for WP7 - Silverlight c#

试试这样做:

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;  
this.textBlock1.Text = value;

由于不能使用xpath,因此必须手动遍历DOM。