如何输出 WP7 的 HTML Agility Pack

发布于 2024-12-02 18:11:34 字数 3775 浏览 2 评论 0原文

我有下面的代码来解析 html 页面的一部分。

我想知道如何将其输出到列表框或文本框。

每当我尝试时,我都会收到未处理的exceltion错误

private void PhoneApplicationPage_Loaded(object sender, 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">&nbsp;</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">&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>
     <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>

I have the below code to parse an section of an html page.

What I would like to know is how to output it to either a listbox or text box.

Whenever I try I get unhandled exceltion error

private void PhoneApplicationPage_Loaded(object sender, 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;

Exception error is :

System.NullReferenceException was unhandled
Message: NullReferenceException

Stack trace is:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2024-12-09 18:11:34

HtmlDocument.Load 函数不将 Uri 作为参数,它应该接收您以某种方式从服务器检索的结构化 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。

HtmlDocument.Load function doesn't take a Uri as a paramter, it should recieve a structured HTML, that you have somehow retrieved from a server.

Here is the code for your case using the WebClient class to retrieve the HTML from server:

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

At the end, the value variable holds the inner HTML you need to further parse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文