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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:22:35+00:00 2026-05-15T13:22:35+00:00

I am serializing data into xml by converting Dictionary into List. The serialization is

  • 0

I am serializing data into xml by converting Dictionary into List.
The serialization is ok.

Is it possible to populate dictionary on deserialization? (right now I populate dictionary after deserialization completes and list is returned )

 [Serializable]
    public class Attribute
    {
        public string Key { get; set; }
        public int Value1 { get; set; }
        public int Value2 { get; set; }
        public int Value3 { get; set; }


        public Attribute() { }
        public Attribute(string key, int value1, int value2, int value3)
        {
            Key = key;
            Value1 = value1;
            Value2 = value2;
            Value3 = value3;
        }
    }

    [XmlRoot("Container")]    
    public class TestObject
    {
        public TestObject() { }
        private Dictionary<string, Attribute> dictionary = new Dictionary<string, Attribute>();


        [XmlIgnore()]
        public Dictionary<string, Attribute> Dictionary
        {
            set { dictionary = value; }
            get { return dictionary; }
        }

        public string Str { get; set; }        


        private List<Attribute> _attributes = new List<Attribute>();       
        public List<Attribute> Attributes
        {
            get 
            {
                if (Dictionary.Count>0)
                {

                    foreach (string key in Dictionary.Keys)
                    {
                        _attributes.Add(new Attribute(key, Dictionary[key].Value1,  Dictionary[key].Value2,    Dictionary[key].Value3));
                    }
                    return _attributes;
                }
            return _attributes;
            }               
        }

    }

Code:

 TestObject TestObj = new TestObject();
 TestObj.Dictionary.Add("asdsad", new Attribute { Value1 = 232, Value2 = 12, Value3 = 89 });
 TestObj.Dictionary.Add("sdfer", new Attribute { Value1 = 10, Value2 = 7, Value3 = 857 });
 TestObj.Dictionary.Add("zxcdf", new Attribute { Value1 = 266, Value2 = 85, Value3 = 11 });                           

 TestObj.Str = "Test";            

 XmlWriterSettings settings = new XmlWriterSettings();
 settings.OmitXmlDeclaration = true;
 settings.Indent = true;
 XmlSerializer serializer = new XmlSerializer(typeof(TestObject));

 using (XmlWriter writer = XmlWriter.Create(@"C:\test.xml", settings))
 {              
      XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
      namespaces.Add(string.Empty, string.Empty);

      serializer.Serialize(writer, TestObj, namespaces);                            
 }

 TestObject newob;
 using (TextReader textReader = new StreamReader(@"C:\test.xml"))
 {
      newob = (TestObject)serializer.Deserialize(textReader);
     //repopulate dictionary from Attribute list
      foreach (Attribute atr in newob.Attributes)
      {
          //code
      }
 }
  • 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-15T13:22:36+00:00Added an answer on May 15, 2026 at 1:22 pm

    If the question is “can I convert a serialized list to a dictionary without writing code” then the answer is no. There isn’t much wrong with what you are doing now.

    You could consider a dictionary that supports XML serialization so you don’t have to convert it to a list first. Here’s one:

      public class XmlDictionary<T, V> : Dictionary<T, V>, IXmlSerializable {
        [XmlType("Entry")]
        public struct Entry {
          public Entry(T key, V value) : this() { Key = key; Value = value; }
          [XmlElement("Key")]
          public T Key { get; set; }
          [XmlElement("Value")]
          public V Value { get; set; }
        }
    
        System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema() {
          return null;
        }
    
        void IXmlSerializable.ReadXml(System.Xml.XmlReader reader) {
          this.Clear();
          var serializer = new XmlSerializer(typeof(List<Entry>));
          reader.Read();
          var list = (List<Entry>)serializer.Deserialize(reader);
          foreach (var entry in list) this.Add(entry.Key, entry.Value);
          reader.ReadEndElement();
        }
    
        void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) {
          var list = new List<Entry>(this.Count);
          foreach (var entry in this) list.Add(new Entry(entry.Key, entry.Value));
          XmlSerializer serializer = new XmlSerializer(list.GetType());
          serializer.Serialize(writer, list);
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 460k
  • Answers 460k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, it's possible. Just give your dialog elements different id's.… May 15, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer An 'associative array' in PHP (i.e. with meaningful keys like… May 15, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer This link provides a solution. Try replacing your newlines with… May 15, 2026 at 11:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.