Har nu under en tid försökt fått igång deserialization av ett egentillverkat Collection objekt som ärver av CollectionBase.
using System;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
namespace iMat
{
[XmlRoot("Dishes")]
public class DishCollection : CollectionBase
{
public void Add(Dish dish)
{
List.Add(dish);
}
public void Remove(Dish dish)
{
List.Remove(dish);
}
public new int Count
{
get { return List.Count; }
}
public Dish this[int Index]
{
get { return (Dish)List[Index]; }
set { List[Index] = value; }
}
public static DishCollection Fill()
{
string FilePath = "dishes.xml";
FileStream fs = null;
DishCollection collection = new DishCollection();
[b]XmlSerializer xmlSer = new XmlSerializer(typeof(DishCollection));[/b]
try
{
fs = new FileStream(FilePath, FileMode.Open);
collection = (DishCollection)xmlSer.Deserialize(fs);
fs.Close();
return collection;
}
catch (System.IO.FileNotFoundException ex)
{
throw new FileNotFoundException("The configuration file is missing", FilePath, ex);
}
finally
{
if (fs != null)
fs.Close();
}
}
public DishCollection()
{ }
}
}
Och XML filen ser ut så här:
<?xml version="1.0" encoding="utf-8" ?>
<Dishes>
<Dish>
<Name>Pasta</Name>
<Lastused>2002-02-11 00:20:00</Lastused>
<Nextuse>2002-02-11 00:20:00</Nextuse>
</Dish>
<Dish>
<Name>Fisk</Name>
<Lastused>2002-02-11 00:20:00</Lastused>
<Nextuse>2002-02-11 00:20:00</Nextuse>
</Dish>
</Dishes>
Felmeddelandet är:
There was an error reflecting 'iMat.DishCollection'.
På raden som står med fet stil ovan.
Någon erfarenhet av detta eller tips? tveka då inte att svara :)