I want to convert my dictionary data into XML. I am trying to use XML serialization but I am getting an error. Below is my code.
class Program
{
static void Main(string[] args)
{
Dictionary<int, AddressDetails> objDic = new Dictionary<int, AddressDetails>();
for (int i = 1; i <= 5; i++)
{
AddressDetails obj = new AddressDetails();
obj.HouseNo = i;
obj.StreetName = "New Street One " + i;
obj.City = "New Delhi One " + i;
objDic.Add(i, obj);
}
string str = Utility.GetXMLFromObject(objDic);
}
}
public class AddressDetails
{
public int HouseNo { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
private string PoAddress { get; set; }
}
public static class Utility
{
public static string GetXMLFromObject(object o)
{
StringWriter sw = new StringWriter();
XmlTextWriter tw = null;
try
{
XmlSerializer serializer = new XmlSerializer(o.GetType());
tw = new XmlTextWriter(sw);
serializer.Serialize(tw, o);
}
catch (Exception ex)
{
//Handle Exception Code
}
finally
{
sw.Close();
if (tw != null)
{
tw.Close();
}
}
return sw.ToString();
}
}
Error Code line : XmlSerializer serializer = new XmlSerializer(o.GetType());
Error Description :
The type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[ConsoleApplication5.AddressDetails, ConsoleApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] is not supported because it implements IDictionary.
Please suggest.