开发者

How to load XML-data into Silverlight application with WebClient?

开发者 https://www.devze.com 2023-03-26 12:43 出处:网络
I have several items in my xml file, but only one and the first loads. I\'m not sure what\'s going on. But when I output the e.Result string all the items show. Somehow the items doesn\'t parse throug

I have several items in my xml file, but only one and the first loads. I'm not sure what's going on. But when I output the e.Result string all the items show. Somehow the items doesn't parse through the xml-file.

<?xml version="1.0" encoding="utf-8" ?>
<Images>
    <Image>hello.jpg</Image>
    <Image>goodbye.jpg</Image>
    <Image>flower.jpg</Image>
    <Image>bird.jpg</Image>
</Images>
开发者_StackOverflow社区

Code:

WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("Data.xml", UriKind.RelativeOrAbsolute));

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    string result = e.Result;
    byte[] byteArray = Encoding.UTF8.GetBytes(result); 
    MemoryStream stream = new MemoryStream(byteArray);

    int index = 0; 
    foreach (XElement element in xdoc.Descendants("Images").ToList())
    {
        index += 1; 
    }
    Label l = new Label();
    l.Content = index.ToString(); 
    listBox1.Items.Add(l); 
}

    // outputs 1 - WTF? 


There's only 1 "Images" element in the XML document, so xdoc.Descendants("Images") will only return 1 element. If you want the individual <Image> elements, you should use xdoc.Descendants("Image") or xdoc.Descendants("Images").Elements()

0

精彩评论

暂无评论...
验证码 换一张
取 消