---
title: "Deserializering av object varav en är en IDictionary"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/84244-deserializering-av-object-varav-en-är-en-idictionary"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "renholm"
published: "2003-08-14T15:18:33.000Z"
updated: "2003-08-14T15:23:03.000Z"
replies: 1
views: 259
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/84244-deserializering-av-object-varav-en-är-en-idictionary"
---

# Deserializering av object varav en är en IDictionary

## #1 — renholm, 2003-08-14T15:18Z

Kan inte få min deserializering av följande object att fungera, får detta felmeddelande

> For non-array types, you may use the following attributes: XmlAttribute, XmlText, XmlElement, or XmlAnyElement.
> 
> Stack Trace: 
> 
> 
> \[InvalidOperationException: For non-array types, you may use the following attributes: XmlAttribute, XmlText, XmlElement, or XmlAnyElement.\]
>    System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType) +11541
>    System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns) +176
>    System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns) +1584
> 
> \[InvalidOperationException: There was an error reflecting property 'Nodes'.\]
>    System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns) +2340
>    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, Boolean repeats) +448
> 
> \[InvalidOperationException: There was an error reflecting type 'Renholm.Lightweight.Business.Website'.\]
>    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, Boolean repeats) +1070
>    System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace) +168
>    System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) +227
>    System.Xml.Serialization.XmlSerializer..ctor(Type type) +27
>    Renholm.Lightweight.Business.Website.Create(CultureInfo culture) in i:\\renholm\\lite\\business\\website.cs:41
>    Renholm.Lightweight.Business.User.get_Website() in I:\\Renholm\\Lite\\Business\\User.cs:19
>    Renholm.Lightweight.UI.Page.Page_Load(Object sender, EventArgs e) in i:\\renholm\\lite\\ui\\page.cs:20
>    System.EventHandler.Invoke(Object sender, EventArgs e) +0
>    System.Web.UI.Control.OnLoad(EventArgs e) +67
>    System.Web.UI.Control.LoadRecursive() +35
>    System.Web.UI.Page.ProcessRequestMain() +731

 

Följande kod:

```
public class Website
	{
		private string title;
		private NodeCollection nodes;

		[XmlAttribute]
		public string Title
		{
			get { return title; }
			set { title = value; }
		}

		[XmlArrayItemAttribute("Node")]
		public NodeCollection Nodes
		{
			get { return nodes; }
			set { nodes = value; }
		}

		public static Website Create(CultureInfo culture)
		{
			string filepath	= ConfigurationSettings.AppSettings["Settings"] + "\\" + culture.Name + "\\Site.xml";
			Website result	= (Website)HttpContext.Current.Cache["Site_" + culture.Name];

			if (result == null)
			{
				using(StreamReader stream = new StreamReader(filepath))
				{
					XmlSerializer xml = new XmlSerializer(typeof(Website));
					result = (Website)xml.Deserialize(stream);

					HttpContext.Current.Cache.Insert("Site_" + culture.Name, result, new CacheDependency(filepath));
				}
			}

			return result;
		}

		public Website()
		{ }
	}

	public class NodeCollection : DictionaryBase, IXmlSerializable
	{
		public NodeCollection()
		{ }
		
		public virtual Node this[string key]
		{
			get { return (Node)Dictionary[key]; }
			set { Dictionary[key] = value; }
		}
		
		public virtual void Add(string key, Node value)
		{
			Dictionary.Add(key, value);
		}
		
		public virtual bool Contains(string key)
		{
			return this.Dictionary.Contains(key);
		}

		public virtual bool ContainsKey(string key)
		{
			return this.Dictionary.Contains(key);
		}
		
		public virtual bool ContainsValue(Node value)
		{
			foreach (Node item in this.Dictionary.Values)
			{
				if (item == value)
					return true;
			}        return false;
		}
		
		public virtual void Remove(string key)
		{
			this.Dictionary.Remove(key);
		}

		[XmlIgnore]
		public virtual ICollection Keys
		{
			get { return this.Dictionary.Keys; }
		}

		[XmlIgnore]
		public virtual ICollection Values
		{
			get	{ return this.Dictionary.Values; }
		}

		void IXmlSerializable.WriteXml(System.Xml.XmlWriter w)
		{
			XmlSerializer xml = new XmlSerializer(typeof(Node));
			foreach (string key in Dictionary.Keys)
			{
				Node value = (Node)Dictionary[key];
				xml.Serialize(w, value);
			}
		}
		
		void IXmlSerializable.ReadXml(System.Xml.XmlReader r)
		{
			XmlSerializer xml = new XmlSerializer(typeof(Node));

			r.Read();
			r.ReadStartElement("Nodes");
			while(r.NodeType != System.Xml.XmlNodeType.EndElement)
			{	
				Node value = (Node)xml.Deserialize(r);
				Dictionary.Add(value.Id, value);
				r.MoveToContent();
			}
			r.ReadEndElement();
		}

		System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
		{
			return null;
		}
	}

public class Node
	{
		private string id;
		private string name;
		private NodeCollection nodes;
		private ModuleCollection modules;

		[XmlAttribute]
		public string Id
		{
			get { return id; }
			set { id = value; }
		}

		[XmlElement]
		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		[XmlArrayItemAttribute("Node")]
		public NodeCollection Nodes
		{
			get { return nodes; }
			set { nodes = value; }
		}

		[XmlArrayItemAttribute("Node")]
		public ModuleCollection Modules
		{
			get { return modules; }
			set { modules = value; }
		}

		public Node()
		{ }
	}

public class ModuleCollection : CollectionBase
	{
		public void Add(Module item)
		{
			List.Add(item);
		}

		public void Remove(Module item)
		{
			List.Remove(item);
		}

		public Module this[int Index]
		{
			get	{ return (Module)List[Index]; }
			set	{ List[Index] = value; }
		}

		public ModuleCollection()
		{ }
	}

	public class Module
	{
		private string panel;
		private string control;

		[XmlElement]
		public string Panel
		{
			get { return panel; }
			set { panel = value; }
		}

		[XmlElement]
		public string Control
		{
			get { return control; }
			set { control = value; }
		}

		public Module()
		{ }
	}
```

XML filen

```
<?xml version="1.0" encoding="iso-8859-1" ?>
<Website Title="Test">
	<Nodes>
		<Node Id="start">
			<Name>Startsidan</Name>
			<Nodes>
				<Node Id="undersida">
					<Name>Start Undersidan</Name>			
				</Node>
			</Nodes>
		</Node>
	</Nodes>
</Website>
```

Är inte direkt säker på vad jag gjort i IXmlSerializable.ReadXml(System.Xml.XmlReader r).

red // skickade med xml-filen också

Permalänk: https://www.webforum.nu/p/84244

## #2 — renholm, 2003-08-14T15:23Z

Tror jag löste de problemet genom att ändra \[XmlArrayItemAttribute("Node")\] på public NodeCollection Nodes till \[XmlElement\] eftersom jag kom på att NodeCollection inte riktigt räknas som en Array då jag fick ta till en speciall lösning i och med att jag behovde ha NodeCollection som ett Dictionary object.

Permalänk: https://www.webforum.nu/p/1138429

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/84244-deserializering-av-object-varav-en-är-en-idictionary
