【发布时间】:2013-08-21 20:35:27
【问题描述】:
我在反序列化对象列表时遇到问题。我只能将一个对象序列化为一个对象,但无法获取列表。我没有收到任何错误,它只是返回一个空列表。这是返回的 XML:
<locations>
<location locationtype="building" locationtypeid="1">
<id>1</id>
<name>Building Name</name>
<description>Description of Building</description>
</location>
</locations>
这是我拥有的类,我在 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<Building> GetAll()
{
var listBuildings = new List<Building>();
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<Building>),
new XmlRootAttribute() { ElementName = "locations" });
listBuildings = (List<Building>)serializer.Deserialize(reader);
return listBuildings;
}
}
【问题讨论】:
-
你遇到了什么错误?
-
哪些列表没有看到序列化?如果是
GetAll()方法,那是因为序列化程序(好吧,可能很少,我猜)不在调用类方法的业务中。他们的职责是保留对象的属性值。 -
我没有收到错误,它只是返回一个空列表。我不是在尝试序列化 GetAll 方法,而是使用 Get All 方法从 Web 服务反序列化并填充该构建对象。
标签: c# xml serialization