【问题标题】:Deserialize XML fails to connect class to elements反序列化 XML 无法将类连接到元素
【发布时间】:2013-05-07 00:51:04
【问题描述】:

我正在尝试将 Web 上的 XML 文档反序列化为 Windows Phone 7 环境的关联类。我已经使用异步来获取文档,它加载到 XmlReader 中,据我可以通过调试器判断,并且反序列化器报告它可以被反序列化。当我尝试反序列化时,我得到一个空类。

这是我想从 Xml 中填充的类:

namespace CineQuest
{
    //[Serializable()]
    //This is the main head of the XML data object
    [XmlRoot("festival")]
    public class Festival
    {
    /* Later */
    //[XmlElement("program_items")]
    //public ProgramItems programItems  { get; set; }

    [XmlElement("films")]
    public Films films { get; set; }

    [XmlElement("schedules")]
    public Schedules schedules { get; set; }

    /* Later */
    //[XmlElement("venue_Locations")]
    //public VenueLocations venueLocations { get; set; }
}

public class Films
{
    [XmlArray("films")]
    [XmlArrayItem("film", typeof(Film))]
    public List<Film> filmsList { get; set; }

    public Films()
    {
        filmsList = new List<Film>();
    }
}

public class Film
{
    [XmlElementAttribute("id")]
    public string id { get; set; }

    [XmlElement("title")]
    public string title { get; set; }

    [XmlElement("description")]
    public string description { get; set; }

    [XmlElement("tagline")]
    public string tagline { get; set; }

    [XmlElement("genre")]
    public string genre { get; set; }

    [XmlElement("imageURL")]
    public string imageURL { get; set; }

    [XmlElement("director")]
    public string director { get; set; }

    [XmlElement("producer")]
    public string producer { get; set; }

    [XmlElement("cinematographer")]
    public string cinematographer { get; set; }

    [XmlElement("editor")]
    public string editor { get; set; }

    [XmlElement("cast")]
    public string cast { get; set; }

    [XmlElement("country")]
    public string country { get; set; }

    [XmlElement("language")]
    public string language { get; set; }

    [XmlElement("film_info")]
    public string film_info { get; set; }
}

public class Schedules
{
    [XmlArray("schedules")]
    [XmlArrayItem("schedule", typeof(Schedule))]
    public List<Schedule> schedulesList { get; set; }

    public Schedules()
    {
        schedulesList = new List<Schedule>();
    }
}

public class Schedule
{
    [XmlElementAttribute("id")]
    public string id { get; set; }

    [XmlElementAttribute("program_item_id")]
    public string programItemId { get; set; }

    [XmlElementAttribute("start_time")]
    public string startTime { get; set; }

    [XmlElementAttribute("end_time")]
    public string endTime { get; set; }

    [XmlElementAttribute("venue")]
    public string venue { get; set; }
}
}

这就是我正在尝试的方式:

XmlSerializer serializer = new XmlSerializer(typeof(Festival));
reader = XmlReader.Create(new StringReader(data.Result));
object deserialization = serializer.Deserialize(reader);
MessageBox.Show(deserialization.ToString());
festival = (Festival)deserialization;

FilmItemList list = new FilmItemList(festival);
list.populateList();         
foreach (FilmItem item in list.Itemlist)
{
    this.Items.Add(new ItemViewModel() { LineOne = item.lineone, LineTwo =    item.linetwo, LineThree = item.linethree, LineFour = item.linefour });
}

我知道我可以像手动完成一样填充类,但我知道我缺少一些东西来将反序列化的 Xml 连接到它们应该连接的类,但我不知道是什么。

【问题讨论】:

    标签: windows-phone-7 serialization xmlreader


    【解决方案1】:

    我相信你必须用Serializable 属性来装饰你所有的类,如果它们要被序列化的话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 2016-12-02
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多