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()
精彩评论