webForumDet fria alternativet

Problem med deserialization

3 svar · 238 visningar · startad av renholm

renholmMedlem sedan apr. 20012 266 inlägg
#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 :)

nikoMedlem sedan juni 20022 599 inlägg
#2

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?

renholmMedlem sedan apr. 20012 266 inlägg
#3

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()
		{
			
		}

	}
}
renholmMedlem sedan apr. 20012 266 inlägg
#4

Problemet är nu löst.
Intresserad av lösningen? Kontakta mig, blir så mycke att posta :)

140 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
137 ms — hämta tråd, inlägg och bilagor (db)