如何将HTML分析(HAP)绑定到ListBox DataTemplate [英] How to Bind HTML Parse (HAP) to ListBox DataTemplate

查看:77
本文介绍了如何将HTML分析(HAP)绑定到ListBox DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在运行下面的代码来使用WP7的HTML敏捷包解析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 =单元(j => j.Attribute(class)==airline)。Value,
FlightType = n.Element(td)。单=> j.Attribute(class)==airline)。Value,
Time = n.Element(td)。Single(j => j.Attribute(class)= =航空公司)价值,
}

);

这会将以下内容输出到滚动查看器中





每行有多个td对于航班,时间,航空公司等等。我想要做的是将每个td内部文本绑定到一个listbox dataTemplate。



下面是我认为XAML看起来像的一个例子

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

< TextBlock Text ={Binding Airline}Foreground =#FF4BCCF5FontSize =24/>
< TextBlock Text ={Binding Flight}TextWrapping =WrapFontSize =22Foreground =#FF969696/>
< TextBlock Text ={Binding Time}TextWrapping =WrapFontSize =20Foreground =#FF05C16C/>
< TextBlock Text ={Binding Origin}TextWrapping =WrapFontSize =20/>
< / StackPanel>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>

HTML示例:

 < tr class => 
< td class =airline>< img src =/ images / airline logos / NZ.giftitle =AIR NEW ZEALAND LIMITED。alt =AIR NEW ZEALAND LIMITED。/> ;< / TD>
< td class =flight> NZ8< / td>
< td class =codeshare>& nbsp;< / td>
< td class =origin>旧金山< / td>
< td class =date> 01九月< / td>
< td class =time> 17:15< / td>
< td class =est> 18:00< / td>
< td class =status> DEPARTED< / td>
< / tr>

所以问题是:如何将每个td结果绑定到它自己的文本块?

解决方案

  public class Flight 
{
公共字符串航空公司{get;组; }
public string Flight {get;组; }
public DateTime Time {get;组; }
}

这是您的代码:

  var node = doc.DocumentNode.Descendants(div)
.FirstOrDefault(x => x.Id ==FlightInfo_FlightInfoUpdatePanel)
.Element(table)
.Element(tbody)
.Elements(tr)。Aggregate(Flight list\\\
,(acc,n)=> acc +\\\
+ n.OuterHtml);

之后

 列表与LT;飞行> flightList = new List< Flight>(); 
foreach(HtmlNode tr in node){
flightList.Add(new Flight(){
Airline = tr.Descendants(td)
.FirstOrDefault(f => InnerHtml,
Flight = tr.Descendants(td)
.FirstOrDefault(f => f.Attributes [class ] .Value ==flight)。InnerText
//在此处插入其他属性
});
}

listbox1.ItemsSource = flightList;


I am currently running the below code to parse an HTML link using HTML Agility Pack for WP7.

EDIT ******************************** Code with suggested changes

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

    );

This outputs the below into a scrollviewer

There are multiple td's in each line for flight, time, airline etc.. What I would like to do is to bind each td inner text to a listbox dataTemplate.

Here is an example of what I think the XAML would look like

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

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

So the question is: How do I bind each td results to its own text block?

解决方案

public class Flight
{    
    public string Airline { get; set; }
    public string Flight { get; set; }
    public DateTime Time { get; set; }    
}

This is your code:

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

After

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;

这篇关于如何将HTML分析(HAP)绑定到ListBox DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆