HTML Agilty for WP7 - Silverlight c#

℡╲_俬逩灬. 提交于 2019-12-14 03:18:16

问题


I am currently trying to parse specfic Tables from a DIV in an HTML doc.

I had this working windows Silverlight, but WP7 HTML agility pack seems to be a different thing altogether.

HTML Looks like this

<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>

I am currently using this code to parse the tables inside the "FlightInfo_FlightInfoUpdatePanel" DIV Box

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; 

This is producing an unhandled exception error:

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)

回答1:


Try something like this then:

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;

Since you can't use xpath, you're forced to manually traverse the DOM.



来源:https://stackoverflow.com/questions/7267961/html-agilty-for-wp7-silverlight-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!