Flippey
Flippey

Reputation: 91

xml array deserialization c#

I've been trying to wrap my head around what's going wrong, but haven't been able to put my finger on it. Maybe you can see where I'm going wrong with deserialising this feed.

From the XML feed I have this structure:

<Recommendation>
  <screenshot-urls>          
    <screenshots d="320x480">
      <screenshot orientation="portrait" w="320" h="480">url1</screenshot>
      <screenshot orientation="portrait" w="320" h="480">url2</screenshot>
      <screenshot orientation="portrait" w="320" h="480">url3</screenshot>
      <screenshot orientation="portrait" w="320" h="480">url4</screenshot>
    </screenshots>
    <screenshots d="480x800">
      <screenshot orientation="portrait" w="480" h="800">url1</screenshot>
      <screenshot orientation="portrait" w="480" h="800">url2</screenshot>
      <screenshot orientation="portrait" w="480" h="800">url3</screenshot>
      <screenshot orientation="portrait" w="480" h="800">url4</screenshot>
    </screenshots>
  </screenshot-urls>
</recommendation>

I've got the following code lined up, I've tried all sorts of things but the closest I got was having an array of 4 screenshots that only used the url of the first screenshot.

public class Recommendation
{
    /// <remarks />
    [XmlElement("screenshot-urls")]
    public TopAppScreenshots[] screenshoturls { get; set; }

    public class TopAppScreenshots
    {
        public TopAppScreenshot[] Screenshots { get; set; }

        public class TopAppScreenshot
        {
            [XmlAttribute("w")]
            public string Width { get; set; }

            [XmlAttribute("h")]
            public string Height { get; set; }

            [XmlAttribute("orientation")]
            public string Orientation { get; set; }

            [XmlText]
            public string ScreenshotUrl { get; set; }
        }
    }
}

At the moment with this code the screenshots object is empty, but looking at other examples I really think this code should work. What am I doing wrong?

Upvotes: 0

Views: 162

Answers (1)

Alexander Petrov
Alexander Petrov

Reputation: 14251

public class Recommendation
{
    [XmlArray("screenshot-urls")]
    [XmlArrayItem("screenshots")]
    public TopAppScreenshots[] Screenshoturls { get; set; }

    public class TopAppScreenshots
    {
        [XmlElement("screenshot")]
        public TopAppScreenshot[] Screenshot { get; set; }

        [XmlAttribute("d")]
        public string Dimension { get; set; }

        public class TopAppScreenshot
        {
            [XmlAttribute("w")]
            public string Width { get; set; }

            [XmlAttribute("h")]
            public string Height { get; set; }

            [XmlAttribute("orientation")]
            public string Orientation { get; set; }

            [XmlText]
            public string ScreenshotUrl { get; set; }
        }
    }
}

Upvotes: 1

Related Questions