471,605 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Generic Dictionary not serializing with the rest of a class...

I have the following classes defined (gets/sets removed for brevity):

[Serializable]
public class Warband
{
public Warband()
{
Contents = new Dictionary<string, WarbandContent>();
}

[XmlElement(ElementName = "Name")]
private string _Name;

[XmlElement(ElementName = "Alignment")]
private string _Alignment;

[XmlElement(ElementName = "BuildTotal")]
private ushort _BuildTotal;

[XmlElement(ElementName = "Activations")]
private ushort _Activations;

[XmlElement(ElementName = "Comments")]
private string _Comments;

[XmlArray(ElementName = "Contents")]
private Dictionary<string, WarbandContent> _Contents;

public void Serialize(string FileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(Warband));
TextWriter writer = new StreamWriter(FileName);

serializer.Serialize(writer, this);
writer.Close();
}
}

[Serializable]
class WarbandContent
{
[XmlElement(ElementName = "MiniatureID")]
private string _MiniatureID;

[XmlElement(ElementName = "Quantity")]
private uint _Quantity;

[XmlElement(ElementName = "Cost")]
private uint _Cost;

[XmlElement(ElementName = "IsCommander")]
private bool _IsCommander;
}

When I call the Serialize method of a Warband object, all properties of that
object are written to the Xml, with the exception of the Dictionary property.
Is there a way to get this Dictionary written to the Xml, or should I start
looking for another way to do this?

Gabe
Jan 12 '06 #1
1 2133
Hello Gabe,
When I call the Serialize method of a Warband object, all properties of that
object are written to the Xml, with the exception of the Dictionary property.
Is there a way to get this Dictionary written to the Xml, or should I start
looking for another way to do this?


Unfortunately there's no way to make XmlSerializer function with
IDictionary-derived objects since the infrastructure explicitly checks for
IDictionary at run time and disables serialization.
But there are some ways around this. One is that you can implement an interface
called IXmlSerializable for the object, but there are also other ways:

http://msdn.microsoft.com/msdnmag/is...3/06/XMLFiles/
(search for "dictionary") [.NET 1.1]

http://blogs.metapropeller.com/klisa...b960c339d.aspx

http://babelfish.altavista.com/babel...25%2F6320.aspx
ciao Frank
--
Dipl.Inf. Frank Dzaebel [MCP/MVP C#]
http://Dzaebel.NET

Jan 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Edward Diener | last post: by
2 posts views Thread by Jasper Kent | last post: by
5 posts views Thread by Andrew Robinson | last post: by
18 posts views Thread by Rune B | last post: by
reply views Thread by crazyone | last post: by
3 posts views Thread by kim.nolsoee | last post: by
7 posts views Thread by =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.