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 7506049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:07:44+00:00 2026-05-29T22:07:44+00:00

I have a class containing a SortedList<string, Data> as private field, where Data is

  • 0

I have a class containing a SortedList<string, Data> as private field, where Data is a simple custom class with some int, DateTime and Nullable<DateTime> fields.

public class CustomCollection
{
    private SortedList<string, Data> _list;

    ...
}

Now I would make my class serializable, so I could write its content (ie the items of the _list field) in an XML file or load data from an existing XML file.

How should I proceed?

I think I understand that there are two ways to serialize: the first would be to mark all fields as serializable, while the second would be to implement the IXmlSerializable interface. If I understand correctly, when I can use each of the two ways?

  • 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-29T22:07:45+00:00Added an answer on May 29, 2026 at 10:07 pm

    Ok, you just need to decorate your Classes with [Serializable] attribute and it should work. However you have a SortedList which implements an IDictionary and these cant be serialized with the IXMLSerializable so need to do a bit of customization look here

    Serializing .NET dictionary

    but if you change your sorted list to a normal list or anything that doesnt implement an IDictionary then the below code will work 🙂 copy it to a console app and run it.

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Data d = new Data { CurrentDateTime = DateTime.Now, DataId = 1 };
                Data d1 = new Data { CurrentDateTime = DateTime.Now, DataId = 2 };
                Data d2 = new Data { CurrentDateTime = DateTime.Now, DataId = 3 };
    
                CustomCollection cc = new CustomCollection
                                          {List = new List<Data> {d, d1, d2}};
    
                //This is the xml
                string xml = MessageSerializer<CustomCollection>.Serialize(cc);
    
                //This is deserialising it back to the original collection
                CustomCollection collection = MessageSerializer<CustomCollection>.Deserialize(xml);
            }
        }
    
        [Serializable]
        public class Data
        {
            public int DataId;
            public DateTime CurrentDateTime;
            public DateTime? CurrentNullableDateTime;
        }
    
        [Serializable]
        public class CustomCollection
        {
            public List<Data> List;
        }
    
        public class MessageSerializer<T>
        {
            public static T Deserialize(string type)
            {
                var serializer = new XmlSerializer(typeof(T));
    
                var result = (T)serializer.Deserialize(new StringReader(type));
    
                return result;
            }
    
            public static string Serialize(T type)
            {
                var serializer = new XmlSerializer(typeof(T));
                string originalMessage;
    
                using (var ms = new MemoryStream())
                {
                    serializer.Serialize(ms, type);
                    ms.Position = 0;
                    var document = new XmlDocument();
                    document.Load(ms);
    
                    originalMessage = document.OuterXml;
                }
    
                return originalMessage;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class Foo , with a private variable bar_ containing some
I have a custom class containing 2 public variables: 1 is a string and
I have a class containing two properties: public class player{ public player(String playerName,int points){
I have a domain class containing a couple of fields. I can access them
I have an example class containing two data points: public enum Sort { First,
We have an entity (class) containing data and methods (lets call it Person). There
I have designed a class containing some information about a given object which will
I have a class MyClass containing a private List<MySecondClass> myList . The list is
So I have a custom type Foo : public class Foo { public string
I have a class Foo, which has a (simple) destructor. Some other class contains

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.