Rhys
Rhys

Reputation: 2877

HTML Parsing c#

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>

Upvotes: 3

Views: 4050

Answers (2)

Damith
Damith

Reputation: 63065

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

Upvotes: 2

Rune FS
Rune FS

Reputation: 21742

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;

Upvotes: 2

Related Questions