我正在尝试将 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 连接到它们应该连接的类,但我不知道是什么。