HTML 解析 C#
我正在解析 HTML 文件并遇到一些问题。
我正在使用以下代码:
编辑************************************
更新的代码现在可以工作。
私有无效PhoneApplicationPage_Loaded(对象发送者,RotedEventArgs 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;
}
}
}
这给了我这个结果。
所有结果现在都按要求显示。
我的问题是:如何更改此代码以显示所有 's
编辑下的所有结果#################### ######### 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>
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.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有与使用 XElement 时相同的方法,这应该可以解决问题
Assuming you have the same methods available on as when using XElement this should do the trick
只需您可以通过以下方式获取所有行
simply you can get all the rows by