我目前正在运行以下代码,使用HTML Agility Pack for WP7解析HTML链接。
编辑********************************代码包含建议的更改
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").Select(n => new Flight()
{
Airline = n.Element("td").Single(j => j.Attribute("class") == "airline").Value,
FlightType = n.Element("td").Single(j => j.Attribute("class") == "airline").Value,
Time = n.Element("td").Single(j => j.Attribute("class") == "airline").Value,
}
);
这将以下内容输出到滚动查看器
航班,时间,航空公司等每行都有多个td。我想做的是将每个td内部文本绑定到列表框dataTemplate。
以下是我认为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 Airline}" Foreground="#FF4BCCF5" FontSize="24" />
<TextBlock Text="{Binding Flight}" TextWrapping="Wrap" FontSize="22" Foreground="#FF969696" />
<TextBlock Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" Foreground="#FF05C16C" />
<TextBlock Text="{Binding Origin}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
HTML示例:
<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>
所以问题是:如何将每个td结果绑定到自己的文本块?
答案 0 :(得分:0)
您可以创建一个绑定到UI的简单模型对象:
public class Flight
{
public string Airline { get; set; }
public string Flight { get; set; }
public DateTime Time { get; set; }
}
然后使用选择投影将您的文档转换为航班列表:
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.OuterHtml);
List<Flight> flights = node.Elements("td").Select(i =>
new Flight()
{
Airline = i.Element("td").Single(j => j.Attribute("class") == "airline").Value,
Flight = i.Element("td").Single(j => j.Attribute("class") == "flight").Value,
...
}
然后将此Flight实例列表绑定到您的UI。
我不熟悉HTML Agility Pack,所以我假设它的Linq API与Linq to XML相似或相同。您可能需要稍微调整一下查询代码,但一般方法应该没问题。
答案 1 :(得分:0)
public class Flight
{
public string Airline { get; set; }
public string Flight { get; set; }
public DateTime Time { get; set; }
}
这是你的代码:
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.OuterHtml);
之后
List<Flight> flightList=new List<Flight>();
foreach (HtmlNode tr in node){
flightList.Add(new Flight(){
Airline=tr.Descendants("td")
.FirstOrDefault(f=>f.Attributes["class"].Value=="airline").InnerHtml,
Flight=tr.Descendants("td")
.FirstOrDefault(f=>f.Attributes["class"].Value=="flight").InnerText
// Insert other properties here
});
}
listbox1.ItemsSource=flightList;