简体   繁体   中英

HTML Agilty for WP7 - Silverlight c#

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM