Question

I am parsing an HTML file and having a few issues.

I am using the below code:

EDIT********************************

Updated Code now working.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {

    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

    client.DownloadStringAsync(new Uri(@"http://www.SourceURL.com"));

}

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")
        .Element("table")
        .Element("tbody")
        .Elements("tr").Aggregate("Flight list\n", (acc, n) => acc + "\n" + n.InnerHtml);
       // .Elements("td")

    this.scrollViewer1.Content = node;




       }

    }
}

This is giving me this result.

enter image description here

All results are now being disaplayed as required.

My Question was : How can I change this code to display all the results under all <tr>'s

edit############################ XAML

ListBox Margin="6,6,-12,0" Name="listBox1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="Auto">

                            <TextBlock Text="{Binding Flight}" Foreground="#FF4BCCF5" FontSize="24" />
                            <TextBlock Text="{Binding Origin}" TextWrapping="Wrap" FontSize="22" Foreground="#FF969696" />
                            <TextBlock Text="{Binding Date}" TextWrapping="Wrap" FontSize="20" Foreground="#FF05C16C" />
                            <TextBlock Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Accepted Answer

Assuming you have the same methods available on as when using XElement this should do the trick

var text = list.Descendants("div")
                 .FirstOrDefault(x => x.Id == "FlightInfo_FlightInfoUpdatePanel")
                 .Element("table")
                 .Element("tbody")
                 .Descendants("tr").Aggregate("",(acc,n)=>acc+"\n"+n.OuterHtml);

 this.textBlock2.Text = text;

Popular Answer

var node = doc.DocumentNode.Descendants("div")
    .FirstOrDefault(x => x.Id == "FlightInfo_FlightInfoUpdatePanel")
    .Element("table")
    .Element("tbody")
    .Descendants("tr").ToArray();

this.textBlock2.Text = string.Join(Environment.NewLine, node.Select(tr => tr.InnerHtml));

simply you can get all the rows by

var node = doc.DocumentNode.Descendants("div")
    .FirstOrDefault(x => x.Id == "FlightInfo_FlightInfoUpdatePanel")
    .Element("table")
    .Element("tbody");

    if (node != null)
    {
       this.textBlock2.Text = node.InnerHtml;
    }



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why