R renholm Medlem sedan apr. 2001 2 266 inlägg Frågan 24 mars 2003 19:49 #1 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
N niko Medlem sedan juni 2002 2 599 inlägg
tveka då inte att svara
Tar dig på orden och spånar utan egentlig koll:
Är XML-filen resultat av en motsvarande serialisering av ett objekt eller har du skrivit den för hand?
Krävs det inte lite ytterligare attribut tex vid public Dish this[int Index]? Typ [XmlAttributeAttribute("Dish")]?
Hur ser definitionen av klassen Dish ut. Ifall man vill testa?
R renholm Medlem sedan apr. 2001 2 266 inlägg XML är skriven för hand och Dish classen ser ut så här:
using System;
using System.Xml.Serialization;
namespace iMat
{
/// <summary>
/// Containing information about the dish.
/// </summary>
public class Dish
{
private string name;
private DateTime lastused;
private DateTime nextuse;
[XmlElement]
private string Name
{
get { return name; }
set { name = value; }
}
[XmlElement]
private DateTime Lastused
{
get { return lastused; }
set { lastused = value; }
}
[XmlElement]
private DateTime Nextuse
{
get { return nextuse; }
set { nextuse = value; }
}
public Dish(string Name, DateTime LastUsed, DateTime NextUse)
{
name = Name;
lastused = LastUsed;
nextuse = NextUse;
}
public Dish()
{
}
}
}
R renholm Medlem sedan apr. 2001 2 266 inlägg Problemet är nu löst.
Intresserad av lösningen? Kontakta mig, blir så mycke att posta