RE: deserializing and serializing generic collections

  • From: "Ken Perry" <whistler@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 18 Dec 2008 19:35:24 -0500

 

 

Its only 77 lines so here it is

 

using System;    

using System.Collections.Generic;    

using System.Text;    

using System.Xml.Serialization; 

    

 

namespace tools

{

[XmlRoot("dictionary")]    

public class SerializableDictionary<TKey, TValue>

        : Dictionary<TKey, TValue>, IXmlSerializable

    {

 

        #region IXmlSerializable Members        

 

public System.Xml.Schema.XmlSchema GetSchema()

        {            

return null;

        }         

 

public void ReadXml(System.Xml.XmlReader reader)

        {            

XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));            

XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));


bool wasEmpty = reader.IsEmptyElement;

 

            reader.Read();             

 

if (wasEmpty)                

return;             

 

while (reader.NodeType != System.Xml.XmlNodeType.EndElement)

            {

 

                reader.ReadStartElement("item"); 

                reader.ReadStartElement("key");

                TKey key = (TKey)keySerializer.Deserialize(reader);

                reader.ReadEndElement(); 

 

                reader.ReadStartElement("value");

                TValue value = (TValue)valueSerializer.Deserialize(reader);

                reader.ReadEndElement();                 

this.Add(key, value); 

                reader.ReadEndElement();

 

                reader.MoveToContent();

            }//while

 

            reader.ReadEndElement();

        }         //ReadXml

 

public void WriteXml(System.Xml.XmlWriter writer)

        {            

XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));            

XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));


 

foreach (TKey key in this.Keys)

            {

                writer.WriteStartElement("item"); 

                writer.WriteStartElement("key");

                keySerializer.Serialize(writer, key);

                writer.WriteEndElement(); 

 

                writer.WriteStartElement("value");

                TValue value = this[key];

                valueSerializer.Serialize(writer, value);

                writer.WriteEndElement(); 

                writer.WriteEndElement();

 

            }//foreach (

 

        }//WriteXml

 

        #endregion

 

 

    }//End class

} // namespace SpeechWrapper.Martialer

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of rick watson
Sent: Thursday, December 18, 2008 7:14 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: deserializing and serializing generic collections

 

Hi Ken,

That would be appreciated.

Thanks rick

 

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Ken Perry
Sent: Wednesday, December 17, 2008 7:34 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: deserializing and serializing generic collections

 

 

I could send you an example of a serializable dictionary I created from
examples on line I don't have a book I could point you at I just did a lot
of searching on google to find the info.

 

Ken

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of rick watson
Sent: Wednesday, December 17, 2008 8:14 AM
To: BlindGeeks@xxxxxxxxxxxxxxx; programmingblind@xxxxxxxxxxxxx
Subject: deserializing and serializing generic collections

 

Good morning listers,

Does anyone out there have pointers for serialization / deserialization of
generic collections in c#.

I wish to create xml for a defined generic collection.

 

Thanks for any pointers.

 

Rick

 

Other related posts: