【问题标题】:xml array deserialization c#xml数组反序列化c#
【发布时间】:2016-07-11 14:49:19
【问题描述】:

我一直在试图弄清楚出了什么问题,但一直无法解决问题。也许您可以看到我在反序列化此提要时出了什么问题。

从 XML 提要中,我有这样的结构:

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

我已经排列了以下代码,我尝试了各种各样的方法,但我得到的最接近的是一组 4 个仅使用第一个屏幕截图的 url 的屏幕截图。

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

目前,使用此代码的屏幕截图对象为空,但查看其他示例,我真的认为此代码应该可以工作。我做错了什么?

【问题讨论】:

  • 试试这个答案中解释的方式:stackoverflow.com/questions/4203540/…
  • 好提示,不知道那个特别贴的东西!确实很简洁,但是还没用,明天再试试看是不是格式不对

标签: c# arrays xml deserialization


【解决方案1】:
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; }
        }
    }
}

【讨论】:

  • 谢谢你,这很完美:) 我也很接近,我在 hte screenshot urls 上有 XMLArray 注释,但我在 Screenshot 对象上有 arrayitem
猜你喜欢
  • 2012-04-10
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多