serialize custom-typed dictionary to xml

J

John Grandy

I wish MyClass to be serializable to xml. What do I need to do to
accomplish this ? Thanks.



public class MyClass
{

Dictionary<MyEnum, MyItem> _myItems;
// etc.

public Dictionary<MyEnum, MyItem> MyItems
{
get { return _myItems; }
}

// etc.
}


public class MyItem
{
MyEnum _enumValue;
string _stringValue;
// etc.

public MyEnum EnumValue
{
get { return _myEnum; }
set { _myEnum = value; }
}

public string StringValue
{
get { return _stringValue; }
set { _stringValue = value; }
}

// etc.
}


public enum MyEnum
{
Value1 = 1,
// etc
}
 
F

Family Tree Mike

I wish MyClass to be serializable to xml. What do I need to do to
accomplish this ? Thanks.



public class MyClass
{

Dictionary<MyEnum, MyItem> _myItems;
// etc.

public Dictionary<MyEnum, MyItem> MyItems
{
get { return _myItems; }
}

// etc.
}


public class MyItem
{
MyEnum _enumValue;
string _stringValue;
// etc.

public MyEnum EnumValue
{
get { return _myEnum; }
set { _myEnum = value; }
}

public string StringValue
{
get { return _stringValue; }
set { _stringValue = value; }
}

// etc.
}


public enum MyEnum
{
Value1 = 1,
// etc
}

Dictionarys are not xmlserializable, but there are a number of options
online as to how to serialize the data. I know that there is a
SerializableDictionary class that has code posted online if you search
for it. You could serialize the individual dictionary items and rebuild
the dictionary on deserialization as another option.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Serialize Color to XML 2
Enum To String 14
Deserialization of invalid enum values 3
IDictionary to/from Xml 1
IComparable and generics 2
XML to Dictionary using LINQ 2
Fill Dictionary in a Fluent Way 4
Best practices 14

Top