Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接到WP7应用程序网址处的XML文件_Xml_Silverlight_Windows Phone 7_Webclient_Windows Phone - Fatal编程技术网

连接到WP7应用程序网址处的XML文件

连接到WP7应用程序网址处的XML文件,xml,silverlight,windows-phone-7,webclient,windows-phone,Xml,Silverlight,Windows Phone 7,Webclient,Windows Phone,在过去的几周里,我一直在努力学习WindowsPhone7编程的细节。我已经学习了大部分的基础知识,但是我一直很难找到一个教程来解释如何使用XML。我想创建一个非常基本的应用程序,它访问一个网址上的XML文件,并在应用程序中将文件中的各种项目显示为文本。我遇到过几本教程,它们似乎都以不同的方式来做,或者没有确切地解释我想做的事情。我不想搜索XML文件,我不想更新它,我只想检索它的内容。XML文件中有“items”,其中有“title”和“description”等类别。我希望应用程序列出所有项目

在过去的几周里,我一直在努力学习WindowsPhone7编程的细节。我已经学习了大部分的基础知识,但是我一直很难找到一个教程来解释如何使用XML。我想创建一个非常基本的应用程序,它访问一个网址上的XML文件,并在应用程序中将文件中的各种项目显示为文本。我遇到过几本教程,它们似乎都以不同的方式来做,或者没有确切地解释我想做的事情。我不想搜索XML文件,我不想更新它,我只想检索它的内容。XML文件中有“items”,其中有“title”和“description”等类别。我希望应用程序列出所有项目,并在每个项目中显示其标题和说明

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

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

这说明了您需要什么。(下面的代码非常类似,无法从他的示例中找到指向源代码的链接。)

该应用程序从web服务(在本例中是从Twitter)检索XML

然后,它将XML解析为一个对象集合

    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; }
}
void twitter\u DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
如果(例如错误!=null)
返回;
XElement xmlTweets=XElement.Parse(e.Result);
var list=新列表();
foreach(xmlTweets.Elements(“{http://www.w3.org/2005/Atom}条目“))
{
var userName=t.Element(“{http://www.w3.org/2005/Atom}作者“)。元素(”{http://www.w3.org/2005/Atom}名称“).Value.Split(“”)[0];
var message=t.Element(“{http://www.w3.org/2005/Atom}所有权、价值;
var imageSource=(从t.Elements中的t2开始(“{http://www.w3.org/2005/Atom}链接“)
其中t2.Attribute(“type”).Value.Contains(“image”)
选择t2.Attribute(“href”).Value.First();
添加(新的TweetViewModel)
{
用户名=用户名,
消息=消息,
ImageSource=ImageSource
});
}
twitterList.ItemsSource=列表;
}
公共类TweetViewModel
{
公共字符串用户名{get;set;}
公共字符串消息{get;set;}
公共字符串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>

它是用tools/SDK的第一个CTP编写的,但希望它仍然足够简单,可以让它正常工作。

更新:

我想你是对的,问题可能是我打电话给GetFloster的时候。这就是我称之为:

"

"

正确的地点叫什么?顺便说一下,我很抱歉代码部分显示不正确


嘿,伙计们!谢谢你的帮助!我之前没有创建帐户,所以这可能会作为一个答案出现,因为我在另一台计算机上。我尽了最大努力将Twitter教程改成我正在尝试的内容。我没有收到任何错误,但它没有在模拟器中显示任何内容。我创建了一个XML文件并将其上传到我的个人网站。不幸的是,我无法让代码示例按钮甚至远程运行良好。所以我很抱歉这看起来很糟糕。XML文件包含以下信息:

<?xml version="1.0" encoding="utf-8" ?>
<roster>
  <person><name>Blake</name><age>25</age></person>
  <person><name>Jane</name><age>29</age></person>
  <person><name>Bryce</name><age>29</age></person>
  <person><name>Colin</name><age>29</age></person>
</roster>

布莱克25
珍妮29
布莱斯29
大肠杆菌29
以下是MainPage.xaml.cs:

private void GetRoster()
    {
        WebClient rstr = new WebClient();

        rstr.DownloadStringCompleted += new DownloadStringCompletedEventHandler(roster_DownloadStringCompleted);
        rstr.DownloadStringAsync(new Uri("http://www.MyPersonalWebsiteURL.com/data.xml"));
    }

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

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

        var list = new List<RosterViewModel>();

        foreach (XElement person in xmlPersons.Elements("person"))
        {
            var name = person.Element("name").Value;
            var age = person.Element("age").Value;


            list.Add(new RosterViewModel
            {
                Name = name,
                Age = age,
             });
        }

        rosterList.ItemsSource = list;
    }


    public class RosterViewModel
    {
        public string Name { get; set; }
        public string Age { get; set; }
    }
}
private void get花名册()
{
WebClient rstr=新的WebClient();
rstr.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(花名册\u DownloadStringCompleted);
rstr.DownloadStringAsync(新Uri(“http://www.MyPersonalWebsiteURL.com/data.xml"));
}
无效花名册\u DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
如果(例如错误!=null)
返回;
XElement xmlPersons=XElement.Parse(e.Result);
var list=新列表();
foreach(xmlPersons.Elements中的XElement person(“person”))
{
变量名称=person.Element(“name”).Value;
var age=个人要素(“年龄”)价值;
列表。添加(新的RostServiceWModel
{
Name=Name,
年龄=年龄,
});
}
rosterList.ItemsSource=列表;
}
公共类RostServiceWModel
{
公共字符串名称{get;set;}
公共字符串年龄{get;set;}
}
}
现在MainPage.xaml:

"


"


然而!当我在模拟器中运行应用程序时,我没有收到任何错误,但没有显示任何内容。我知道解决办法可能很简单,所以我想重申一下你的帮助对我来说有多重要。非常感谢您提供的任何建议。

这看起来不错,丹。我为这个问题链接的示例是两个独立的应用程序。在这里,您可以在一个应用程序中获得web服务调用和xml解析/显示。请注意,如果您的web服务调用非常重要,那么您将希望查看HttpWebRequest,以避免在执行过程中对UI交互产生负面影响
<?xml version="1.0" encoding="utf-8" ?>
<roster>
  <person><name>Blake</name><age>25</age></person>
  <person><name>Jane</name><age>29</age></person>
  <person><name>Bryce</name><age>29</age></person>
  <person><name>Colin</name><age>29</age></person>
</roster>
private void GetRoster()
    {
        WebClient rstr = new WebClient();

        rstr.DownloadStringCompleted += new DownloadStringCompletedEventHandler(roster_DownloadStringCompleted);
        rstr.DownloadStringAsync(new Uri("http://www.MyPersonalWebsiteURL.com/data.xml"));
    }

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

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

        var list = new List<RosterViewModel>();

        foreach (XElement person in xmlPersons.Elements("person"))
        {
            var name = person.Element("name").Value;
            var age = person.Element("age").Value;


            list.Add(new RosterViewModel
            {
                Name = name,
                Age = age,
             });
        }

        rosterList.ItemsSource = list;
    }


    public class RosterViewModel
    {
        public string Name { get; set; }
        public string Age { get; set; }
    }
}
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox HorizontalAlignment="Left" Name="rosterList" ItemsSource="rosterList" VerticalAlignment="Top" Width="476">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Height="132">

                        <StackPanel Width="370">
                            <TextBlock Text="{Binding Name}" Foreground="#FFC8AB14" FontSize="28" />
                            <TextBlock Text="{Binding Age}" TextWrapping="Wrap" FontSize="24" />
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>