473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2194
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edward Diener | last post by:
Version 2.0 of the Python database API was written over 5 years ago, in 1999. While it has been used successfully by many implementations, there is no generic access into the data dictionary of...
2
by: Jasper Kent | last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to avoid long instantiation names. I can do this okay: using BigDictionary = System.Collections.Generic.Dictionary<int,...
5
by: Andrew Robinson | last post by:
I need to serialize a dictionary so I can encode the contents. I have the following working but the size seems large. I am guessing that I am serializing the entire object not just the data. Is...
18
by: Rune B | last post by:
Hi Group I was considering using a Generic Dictionary<> as a value container inside my business objects, for the reason of keeping track of fields changed or added and so on. - But how...
3
by: fstorck | last post by:
Hi, I'm kind of stuck with an serializing / deserializing problem using a generic dictionary holding references to various generic types. It goes as follows: <code> class MyBase :...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
3
by: kim.nolsoee | last post by:
Hi I want to use the Dictionary Classs that will load my own class called KeyClass used as TKey. Here is the code: public class Dictionary { public static void Main()
5
by: Steve K. | last post by:
I know this code is garbage but it should give you an idea of what I'm after: <code> class Test { Type _t = null; public void SetSourceList<T>(Dictionary<string, TsourceList) { _t =...
7
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I have a Class "Multidictionary" which I created which is basically a dictionary which can have multiple values for the same key. The class centers around private Dictionary<K,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.