我不认为你可以指示串行器吐出一个IList。序列化器可以创建一个MyProduct集合对象并用Products填充它。这听起来像你想做的事情。
您也可以使用LINQ查询XML文档并创建一个IEnumerable列表。然后
public static IEnumerable<T> DeserializeMany<T>(
string fileName, string descendentNodeName = null) {
descendentNodeName = descendentNodeName ?? typeof(T).Name;
var serializer = new XmlSerializer(typeof(T));
return
from item in XDocument.Load(fileName).Descendants(descendentNodeName)
select (T)serializer.Deserialize(item.CreateReader());
}
,让你列表:
// load from stream if that is the case
// this just uses a file for demonstration purposes
XDocument doc = XDocument.Load("location_of_source.xml");
// select all Product nodes from the root node and create a new Product object using
// object initialization syntax
var listOfProduct = doc.Descendants("Product")
.Select(p => new Product { Name = p.Attribute("Name").Value});
可能重复这个问题:http://stackoverflow.com/questions/226599/deserializing-xml-to-objects-in-c – CodingGorilla 2010-11-09 16:03:07