När jag försöker Deserialize website classen nedan får jag förljande felmeddelande. Deserializering av ett object som innehåller en DictionaryBase.
<code>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
</code>
När jag ändrar till att ha [XmlElement] i stället för [XmlArrayItem] får jag istället ett annat felmeddelande som lyder:
<code>
XML attributes may not be specified for the type Renholm.Lightweight.Business.NodeCollection.
</code>
Eftersom ett Dictionary object inte kan Serializeras som t.ex ett ICollection fick jag lov att göra en speciall lösning som jag tvivlar på blev rätt. Någon som har något tips/lösning?
Koden:
<code>
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()
{ }
}
</code>
XML filen
<code>
<?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>
</code>