简体   繁体   中英

Hi, How to parse xml with descendants within descendants C#

I am trying to parse this xml and can successfully get the name and description but putting together the image is tricky. Both separate code parses work but I'd like to do one parse so I can bind them all to one listbox.

The xml looks like this

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

My code works for both but separately. How do I combine this into one parse...

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

The data displays in the listbox but with the stickers above the description and not side by side.

Thanks


Thanks to Thomas below, the following code works but some user profiles get a Null Referenced Exception (including my own profile which obviously has data) Thanks Thoamas for the assistance perhaps this is an unrelated bug?

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;

If I understood the question correctly, this should do what you want:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM