Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 839067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:24:25+00:00 2026-05-15T05:24:25+00:00

Hi I am trying to serialize a hash table but not happening private void

  • 0

Hi I am trying to serialize a hash table but not happening

private void Form1_Load(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();        

            DateTime dt = DateTime.Now;
            for (int i = 0; i < 10; i++)
                ht.Add(dt.AddDays(i), i);           
            SerializeToXmlAsFile(typeof(Hashtable), ht);
        }

private void SerializeToXmlAsFile(Type targetType, Object targetObject)
        {
            try
            {
                string fileName = @"C:\output.xml";
                //Serialize to XML
                XmlSerializer s = new XmlSerializer(targetType);
                TextWriter w = new StreamWriter(fileName);
                s.Serialize(w, targetObject);
                w.Flush();
                w.Close();
            }
            catch (Exception ex) { throw ex; }
        }

After a google search , I found that objects that impelment IDictonary cannot be serialized. However, I got success with binary serialization.

But I want to have xml one. Is there any way of doing so?

I am using C#3.0

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:24:26+00:00Added an answer on May 15, 2026 at 5:24 am

    First of all starting with C# 2.0 you can use type safe version of very old Hashtable which come from .NET 1.0. So you can use Dictionary<DateTime, int>.

    Starting with .NET 3.0 you can use DataContractSerializer. So you can rewrite you code like following

    private void Form1_Load(object sender, EventArgs e)
        {
            MyHashtable ht = new MyHashtable();        
    
            DateTime dt = DateTime.Now;
            for (int i = 0; i < 10; i++)
                ht.Add(dt.AddDays(i), i);           
            SerializeToXmlAsFile(typeof(Hashtable), ht);
        }
    

    where SerializeToXmlAsFile and MyHashtable type you define like following:

    [CollectionDataContract (Name = "AllMyHashtable", ItemName = "MyEntry",
                             KeyName = "MyDate", ValueName = "MyValue")]
    public class MyHashtable : Dictionary<DateTime, int> { }
    
    private void SerializeToXmlAsFile(Type targetType, Object targetObject)
        {
            try {
                string fileName = @"C:\output.xml";
                DataContractSerializer s = new DataContractSerializer (targetType);
                XmlWriterSettings settings = new XmlWriterSettings ();
                settings.Indent = true;
                settings.IndentChars = ("    ");
                using (XmlWriter w = XmlWriter.Create (fileName, settings)) {
                    s.WriteObject (w, targetObject);
                    w.Flush ();
                }
            }
            catch (Exception ex) { throw ex; }
        }
    

    This code produce C:\output.xml file with the following contain:

    <?xml version="1.0" encoding="utf-8"?>
    <AllMyHashtable xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://schemas.datacontract.org/2004/07/DataContractXmlSerializer">
        <MyEntry>
            <MyDate>2010-06-09T22:30:00.9474539+02:00</MyDate>
            <MyValue>0</MyValue>
        </MyEntry>
        <MyEntry>
            <MyDate>2010-06-10T22:30:00.9474539+02:00</MyDate>
            <MyValue>1</MyValue>
        </MyEntry>
        <!-- ... -->
    </AllMyHashtable>
    

    So how we can see all names of the elements of the destination XML files we can free define.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to serialize some Linq objects using this code. private byte[] GetSerializedObj(object
I've been trying to serialize an object to xml and then hash the result
I am trying to serialize a hash table using the link XML serialization of
I'm trying to serialize a Type object in the following way: Type myType =
I am trying to serialize an object to a sql compact database. I am
I'm trying to serialize an object to XML that has a number of properties,
I have been trying to serialize a pretty big object. This object uses dictionaries
I've run into some problems when trying to serialize my object to XML. The
im trying to serialize some object as xml and then read it back the
I am trying to serialize a C# object to JSON using JSON.net library. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.