连接到 WP7 应用程序网址处的 XML 文件 [英] Connect to XML file at web address for WP7 app

查看:33
本文介绍了连接到 WP7 应用程序网址处的 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几周,我一直在努力学习 Windows Phone 7 编程的来龙去脉.我已经学习了大部分基础知识,但是我一直无法找到一个教程来准确解释如何使用 XML 做某事.我想创建一个非常基本的应用程序,该应用程序访问位于 Web 地址的 XML 文件,并将文件中的各种项目显示为应用程序中的文本.我遇到了几个教程,它们似乎都以不同的方式来做,或者没有准确解释我想要做的事情.我不想搜索 XML 文件,我不想更新它,我只想检索它的内容.XML 文件中有项目",其中有标题"和描述"等类别.我希望该应用列出所有项目,并在每个项目中显示其标题和说明.

I've been trying to learn the ins-and-outs of Windows Phone 7 programming over the past few weeks. I have learned most of the basics but I've been having trouble finding a tutorial explaining exactly how to do something with XML. I want to create a very basic app which accesses an XML file at a web address and displays the various items within the file as text within the app. I've come across several tutorials which all seem to do it in a different way, or aren't explaining exactly the thing it is I want to do. I don't want to search the XML file, I don't want to update it, I just want to retrieve it's contents. Within the XML file are "items" and within those are categories like "title" and "description". I want the app to list all of the items and within each one display it's title and description.

更具体地说,我知道我使用 {Binding Title} 或 {Binding Description} 将内容绑定到文本块.我只是不确定如何使用 WebClient 或任何最简单的方法连接到文件.我可以正常显示解决方案资源管理器中已有的脱机 XML 文件的内容.

To be more specific I know that I bind the contents to textblocks using {Binding Title} or {Binding Description}. I'm just not sure how to connect to the file using WebClient or whatever the easiest method is. I have no problem displaying the contents of an offline XML file that is already in my solution explorer.

我相信有一种非常简单的方法可以做到这一点,我非常感谢您的所有帮助.

I'm sure there is a very simple way to do this, and I really appreciate all of your help.

推荐答案

ScottGu 创建了一个应用程序,它演示了您的需求.(下面的代码非常相似,因为无法从他的示例中找到指向源代码的链接.)

ScottGu created an app which demonstrates what you need. (Code below is very similar as couldn't find link to source from his example.)

该应用从网络服务(在本例中为 Twitter)检索 XML.

The app retrieves XML from a web service (in this case from Twitter.)

    private void GetTweets()
    {
        WebClient twtr = new WebClient();

        twtr.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
        twtr.DownloadStringAsync(new Uri("http://search.twitter.com/search.atom?&q=searchterm"));
    }

然后将 XML 解析为对象集合.

It then parses the XML into a collection of object.

    void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        XElement xmlTweets = XElement.Parse(e.Result);

        var list = new List<TweetViewModel>();

        foreach (XElement t in xmlTweets.Elements("{http://www.w3.org/2005/Atom}entry"))
        {
            var userName = t.Element("{http://www.w3.org/2005/Atom}author").Element("{http://www.w3.org/2005/Atom}name").Value.Split(' ')[0];
            var message = t.Element("{http://www.w3.org/2005/Atom}title").Value;
            var imageSource = (from t2 in t.Elements("{http://www.w3.org/2005/Atom}link")
                               where t2.Attribute("type").Value.Contains("image")
                               select t2.Attribute("href").Value).First();

            list.Add(new TweetViewModel
                    {
                        UserName = userName,
                        Message = message,
                        ImageSource = imageSource
                    });
        }

        twitterList.ItemsSource = list;
    }


public class TweetViewModel
{
    public string UserName { get; set; }
    public string Message { get; set; }
    public string ImageSource { get; set; }
}

然后绑定到一个列表.

<ListBox HorizontalAlignment="Left" Name="twitterList" VerticalAlignment="Top" Width="476">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Height="132">
                <Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
                <StackPanel Width="370">
                    <TextBlock Text="{Binding UserName}" Foreground="#FFC8AB14" FontSize="28" />
                    <TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

它是用工具/SDK 的第一个 CTP 编写的,但希望它仍然足够简单以使其正常工作.

It was written with the first CTP of the tools/SDK but hopefully it should still be simple enough to get this working.

这篇关于连接到 WP7 应用程序网址处的 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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