Can you serialize a Dictionary using XMLSerializer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U2hhd24=?=

    Can you serialize a Dictionary using XMLSerializer?

    Hi;
    I would like to be able to use the XMLSerializer to serialize and
    deserialize a dictionary. is that possible? i know that you can serialize an
    object that implements the ICollection interface. does the fact that
    Dictionary inherits from ICollection<cau se a problem?
    Currently i am using a class that inherits from ICollection and it has a
    Dictionary as it's container. but when it gets to creating the serialized
    object it blows up. here is the code for the collection class:

    using System;
    using System.Collecti ons;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Xml;
    using System.Xml.Seri alization;
    namespace testBed
    {
    [Serializable]

    public class ItemFile : ICollection
    {

    private Dictionary<obje ct, ItemADList = new Dictionary<obje ct,
    Item>();

    public Item this[object key]
    {
    get { return (Item)ADList[key]; }
    }
    public void Add(Item entry)
    {
    ADList.Add(entr y.GetProperty(" ID"), entry);
    }
    public void Clear()
    {
    ADList.Clear();
    }
    public int Count
    {
    get { return ADList.Count; }
    }

    public void CopyTo(Array a, int index)
    {
    foreach(Item item in ADList.Values)
    {
    a.SetValue(item , index);
    index++;
    }
    }
    public object SyncRoot
    {
    get { return this; }
    }
    public bool IsSynchronized
    {
    get { return false; }
    }
    public IEnumerator GetEnumerator()
    {
    return ADList.GetEnume rator();
    }
    }
    }

    here is the code i use to read and write:

    private static void read(string filename, out ItemFile file)
    {
    XmlSerializer serializer = new XmlSerializer(t ypeof(ItemFile) );
    FileStream fs = new FileStream(file name, FileMode.Open);
    file = (ItemFile)seria lizer.Deseriali ze(fs);

    }
    private static void write(string filename, ItemFile file)
    {
    XmlSerializer serializer = new XmlSerializer(t ypeof(ItemFile) );
    TextWriter writer = new StreamWriter(fi lename);
    serializer.Seri alize(writer, file);
    writer.Close();
    }

    any help would be appreciated.
    thanks.
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Can you serialize a Dictionary using XMLSerializer?

    On May 5, 8:22 am, Shawn <Sh...@discussi ons.microsoft.c omwrote:
    Hi;
    I would like to be able to use the XMLSerializer to serialize and
    deserialize a dictionary. is that possible?
    Hi,

    I think that is depends of the key/value types, if you are using a
    non serializable type you will not be able to serialize it

    Comment

    • sloan

      #3
      Re: Can you serialize a Dictionary using XMLSerializer?


      See

      http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!114.entry

      I have not "2.0'ed it up", but maybe it'll help.





      "Shawn" <Shawn@discussi ons.microsoft.c omwrote in message
      news:B0BB6EDD-200D-446B-802E-BC0D4E643118@mi crosoft.com...
      Hi;
      I would like to be able to use the XMLSerializer to serialize and
      deserialize a dictionary. is that possible? i know that you can serialize
      an
      object that implements the ICollection interface. does the fact that
      Dictionary inherits from ICollection<cau se a problem?
      Currently i am using a class that inherits from ICollection and it has a
      Dictionary as it's container. but when it gets to creating the serialized
      object it blows up. here is the code for the collection class:
      >
      using System;
      using System.Collecti ons;
      using System.Collecti ons.Generic;
      using System.Text;
      using System.Xml;
      using System.Xml.Seri alization;
      namespace testBed
      {
      [Serializable]
      >
      public class ItemFile : ICollection
      {
      >
      private Dictionary<obje ct, ItemADList = new Dictionary<obje ct,
      Item>();
      >
      public Item this[object key]
      {
      get { return (Item)ADList[key]; }
      }
      public void Add(Item entry)
      {
      ADList.Add(entr y.GetProperty(" ID"), entry);
      }
      public void Clear()
      {
      ADList.Clear();
      }
      public int Count
      {
      get { return ADList.Count; }
      }
      >
      public void CopyTo(Array a, int index)
      {
      foreach(Item item in ADList.Values)
      {
      a.SetValue(item , index);
      index++;
      }
      }
      public object SyncRoot
      {
      get { return this; }
      }
      public bool IsSynchronized
      {
      get { return false; }
      }
      public IEnumerator GetEnumerator()
      {
      return ADList.GetEnume rator();
      }
      }
      }
      >
      here is the code i use to read and write:
      >
      private static void read(string filename, out ItemFile file)
      {
      XmlSerializer serializer = new XmlSerializer(t ypeof(ItemFile) );
      FileStream fs = new FileStream(file name, FileMode.Open);
      file = (ItemFile)seria lizer.Deseriali ze(fs);
      >
      }
      private static void write(string filename, ItemFile file)
      {
      XmlSerializer serializer = new XmlSerializer(t ypeof(ItemFile) );
      TextWriter writer = new StreamWriter(fi lename);
      serializer.Seri alize(writer, file);
      writer.Close();
      }
      >
      any help would be appreciated.
      thanks.

      Comment

      Working...