1

我正在尝试解析此 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,以下代码有效,但一些用户配置文件得到空引用异常(包括我自己的配置文件,显然有数据) 感谢 Thomas 的帮助也许这是一个不相关的错误?

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;
4

1 回答 1

0

如果我正确理解了这个问题,这应该可以满足您的要求:

XDocument streamFeed = XDocument.Load(new StringReader(response.Content));
var query =
    from sticker in streamFeed.Root.Elements("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;
于 2012-07-16T00:08:07.733 回答