反序列化对象列表的问题

我无法反序列化对象列表。 我只能得到一个对象序列化到一个对象但无法获取列表。 我没有得到错误它只返回一个空列表。 这是返回的XML:

  1 Building Name Description of Building   

这是我的类,我在GetAll方法中反序列化:

 [Serializable()] [XmlRoot("location")] public class Building { private string method; [XmlElement("id")] public int LocationID { get; set; } [XmlElement("name")] public string Name { get; set; } [XmlElement("description")] public string Description { get; set; } [XmlElement("mubuildingid")] public string MUBuildingID { get; set; } public List GetAll() { var listBuildings = new List(); var building = new Building(); var request = WebRequest.Create(method) as HttpWebRequest; var response = request.GetResponse() as HttpWebResponse; var streamReader = new StreamReader(response.GetResponseStream()); TextReader reader = streamReader; var serializer = new XmlSerializer(typeof(List), new XmlRootAttribute() { ElementName = "locations" }); listBuildings = (List)serializer.Deserialize(reader); return listBuildings; } } 

试试这个:

 [XmlRoot("locations")] public class BuildingList { public BuildingList() {Items = new List();} [XmlElement("location")] public List Items {get;set;} } 

然后反序列化整个BuildingList对象。

 var xmlSerializer = new XmlSerializer(typeof(BuildingList)); var list = (BuildingList)xmlSerializer.Deserialize(xml); 

我知道这是一个古老的(呃)问题,但今天我在努力解决这个问题,并找到了一个不需要封装的答案。

假设1:您可以控制源Xml及其构造方式。

假设2:您正在尝试将Xml直接序列化为List对象

  1. 您必须将Xml中的Root元素命名为ArrayOfxxx ,其中xxx是您的类的名称(或XmlType中指定的名称(请参阅2.))
  2. 如果希望xml元素具有与该类不同的名称,则应在该类上使用XmlType

注意:如果您的类型名称(或类名称)以小写字母开头,则应将第一个字符转换为大写。

示例1 – 没有XmlType

 class Program { static void Main(string[] args) { //String containing the xml array of items. string xml = @"  John Doe   Martha Stewart  "; List items = null; using (var mem = new MemoryStream(Encoding.Default.GetBytes(xml))) using (var stream = new StreamReader(mem)) { var ser = new XmlSerializer(typeof(List)); //Deserialising to List items = (List)ser.Deserialize(stream); } if (items != null) { items.ForEach(I => Console.WriteLine(I.Name)); } else Console.WriteLine("No Items Deserialised"); } } public class Item { public string Name { get; set; } } 

示例2 – 使用XmlType

 class Program { static void Main(string[] args) { //String containing the xml array of items. //Note the Array Name, and the Title case on stq. string xml = @"  John Doe   Martha Stewart  "; List items = null; using (var mem = new MemoryStream(Encoding.Default.GetBytes(xml))) using (var stream = new StreamReader(mem)) { var ser = new XmlSerializer(typeof(List)); //Deserialising to List items = (List)ser.Deserialize(stream); } if (items != null) { items.ForEach(I => Console.WriteLine(I.Name)); } else Console.WriteLine("No Items Deserialised"); } } [XmlType("stq")] public class Item { public string Name { get; set; } } 

不确定Building是如何与xml中的位置对应的,但对我来说,如果它们被等同命名则更有意义。 而不是使用List使用LocationList,它变成:

 [Serializable()] [XmlRoot("locations")] public class LocationCollection{ [XmlElement("location")] public Location[] Locations {get;set;} } [Serializable()] [XmlRoot("location")] public class Location { [XmlElement("id")] public int LocationID { get; set; } [XmlAttribute("locationtype")] public string LocationType {get;set;} [XmlElement("name")] public string Name { get; set; } [XmlElement("description")] public string Description { get; set; } [XmlElement("mubuildingid")] public string MUBuildingID { get; set; } } 

然后,您可以按如下方式反序列化:

 var request = WebRequest.Create(method) as HttpWebRequest; var response = request.GetResponse() as HttpWebResponse; var streamReader = new StreamReader(response.GetResponseStream()); TextReader reader = streamReader; var serializer = new XmlSerializer(typeof(LocationCollection), new XmlRootAttribute() { ElementName = "locations" }); var listBuildings = (LocationCollection)serializer.Deserialize(reader); return listBuildings; 

我知道,这是一个古老的问题,但遇到类似的问题却遇到了它。 基于@ ricovox的答案以及OP问题的上下文,这是我用来序列化他的xml的模型:

 [Serializable, XmlRoot("locations")] public class BuildingList { [XmlArrayItem("location", typeof(Building))] public List locations { get; set; } } [Serializable] public class Building { public int LocationID { get; set; } public string Name { get; set; } public string Description { get; set; } public string MUBuildingID { get; set; } public List GetAll() { ... } } 

OP的错误是将列表项创建为根

将[XMLArray]用于集合属性。