serialize custom-typed dictionary to xml

  • Thread starter Thread starter John Grandy
  • Start date Start date
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
}
 
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.
 
Back
Top