473,782 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jun 27 '08 #1
2 5745
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
Jun 27 '08 #2

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" <Sh***@discussi ons.microsoft.c omwrote in message
news:B0******** *************** ***********@mic rosoft.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.

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5430
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not create duplicate XML representations of the shared object, but instead use IDREFs to refer to the shared object. The XML I'm trying to produce is as follows (where "href" is an IDREF): <?xml version="1.0" encoding="utf-8"?> <MyRootClass...
1
6028
by: Daniel Lim | last post by:
When using XmlSerializer, I notice that it does not normalize the single quote and double quote characters, i.e. does not change ' to &apos; and " to &quot. However, it does normalize other characters, like changing ampersand to &amp; Here is my code: ---- start of code -------- MyClass *myClass = MyClass() ; XmlSerializer *xs = new XmlSerializer( __typeof( MyClass ) ) ;
2
1562
by: yinjennytam | last post by:
Hi all I'm learning XML and .NET and I saw an example of using XmlSerializer to deserialize an XML file to get the corresponding object in memory. I found it very useful for my purpose. However, for simple properties it works for me, but I actually need a bit more than just properties. For example, my class actually contains a collection of strings as well, now is using XmlSerializer the right choice? I don't know how many there...
1
1794
by: Sakharam Phapale | last post by:
Hi All, I am working on Text editor. We are using Microsoft Word object for Spelling checking and correcting. Now I want to add new word in Word dictionary using Word object. Can anyone guide me how to achieve my goal. Any suggestions will be appreciated. Thanks and Regards.
0
1077
by: Sakharam Phapale | last post by:
I am working on Text editor. We are using Microsoft Word object for Spelling checking and correcting. Now I want to add new word in Word dictionary using Word object. Can anyone guide me how to achieve my goal. Any suggestions will be appreciated. Thanks and Regards. Sakharam Phapale
0
1196
by: swatisahasrabudhe | last post by:
I have a class that I serialize/deserialize using xmlserializer. There are a few properties with char datatype. I have a few xml files from which I want to create the objects. While deserializing from xml if the properties having char datatypes have numeric values(0-9) then everything worls fine. If I put any letter e.g. A it starts throwing exception - "Input string is not in correct format". If I convert these properties to string, all...
1
5124
by: Yewen Tang | last post by:
I have a schema file datamodel.xsd, element "properties" is declared as a type of "baseProperty". The schema file also defines "derivedProperty" is a derived type of "baseProperty". <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myns="uri:myschema" targetNamespace="uri:myschema" elementFormDefault="qualified" version="1.0"> <xs:complexType name="derivedProperty">
10
4789
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the value for the parameter of an SqlCommand for inserting the xml document in a column of a table where the column is typed to be of the same xml schema. This all sounds simple, but SQL Server REQUIRES the timezone to be specified for date values....
10
2542
by: islayer | last post by:
i am trying to write a dictionary using perl, the program would use a primary .pl file, and a text file, designated .dat i have no programing background, and am trying to teach myself perl, and c++, so excuse my ignorance. i have experimented with some scripts, i have figured out why my last one didn't work, but i have no idea what to do. this was my last attempt: dictionary.pl #!/usr/bin/perl
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.