我想解析这个XML,并可以成功获取名称和说明,但放在一起的图像是棘手的。两个单独的代码解析工作,但我想做一个解析,所以我可以将它们全部绑定到一个列表框。嗨,如何解析与子孙C后裔xml#
的XML看起来像这样
<stickers>
<sticker>
<imageData>
<baseImageUrl> ... want this </baseImageUrl>
<sizes>
<size> don't care about this </size>
</size>
<imageUrlSuffix> ..want this </imageUrlSuffix>
</imageData>
<description>.... want this </description>
<name> --want this </name>
<sticker>
<stickers>
我的代码同时适用于而是分开。如何合并到这一个解析...
XDocument streamFeed = XDocument.Load(new StringReader(response.Content));
var imagedata = from query in streamFeed.Descendants("sticker").Elements("imageData")
select new Stickers
{
image = (string)query.Element("baseImageUrl") + "huge" + (string)query.Element("imageUrlSuffix"),
};
var data = from query in streamFeed.Descendants("sticker")
select new Stickers
{
name = (string)query.Element("name"),
description = (string)query.Element("description"),
};
stickersListBox.ItemsSource = imagedata.Union(data);
的数据显示在列表框中,但上面并排的描述,而不是侧面贴纸。
感谢
感谢Thomas下面,下面的代码工作,但一些用户配置文件得到一个空引用的异常(包括我自己的个人资料,这显然有数据)感谢Thoamas的援助也许这是一个无关错误?
XDocument streamFeed = XDocument.Load(new StringReader(response.Content));
var query =
from sticker in streamFeed.Root.Descendants("sticker")
let imageData = sticker.Element("imageData")
select new Stickers
{
name = (string)sticker.Element("name"),
description = (string)sticker.Element("description"),
image = (string)imageData.Element("baseImageUrl") + "huge" + (string)imageData.Element("imageUrlSuffix")
};
stickersListBox.ItemsSource = query;
这听起来像XML是被解析就好了。你说所有的数据都显示出来了,但不是你希望显示的方式? – 2012-07-15 23:47:07
@MichaelGraczyk是的,它解析正确,但没有正确显示,因为数据被添加到列表后的图像作为单独的对象,而不是与(图像+名称+描述)的对象 – user1502412 2012-07-16 00:29:52