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

  • Home
  • SEARCH
  • 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 7839899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:35:29+00:00 2026-06-02T15:35:29+00:00

I am trying to do a very simple bit of serialization with XmlSerializer: public

  • 0

I am trying to do a very simple bit of serialization with XmlSerializer:

public struct XmlPerson
{
    [XmlAttribute] public string Id   { get; set; }
    [XmlAttribute] public string Name { get; set; }
}

public class GroupOfPeople
{
    private Dictionary<string, string> _namesById = new Dictionary<string, string>();

    //pseudo property for serialising dictionary to/from XML
    public List<XmlPerson> _XmlPeople
    {
        get
        {
            var people = new List<XmlPerson>();
            foreach (KeyValuePair<string, string> pair in _namesById )
                people.Add(new XmlPerson() { Id = pair.Key, Name = pair.Value });

            return people;
        }
        set
        {
            _namesById.Clear();
            foreach (var person in value)
                _namesById.Add(person.Id, person.Name);
        }
    }     
} 

Saving this class works fine, and I get:

<?xml version="1.0" encoding="utf-8"?>
<GroupOfPeople xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <_XmlPeople>
        <XmlPerson Id="person1" Name="Fred" />
        <XmlPerson Id="person2" Name="Bill" />
        <XmlPerson Id="person3" Name="Andy" />
        <XmlPerson Id="person4" Name="Nagesh" />
    </_XmlPeople>
</GroupOfPeople>

However, when I read in the file again, my _XmlPeople property setter is never called, and thus the dictionary is empty. All other properties on this object get deserialized fine.

Am I missing something obvious? I have tried various collection types but none of them deserialize.

EDIT: Read code:

try
{
    using (var stream = new StreamReader(itemPath))
    {
        var xml = new XmlSerializer(typeof(GroupOfPeople));
        GroupOfPeople item = (GroupOfPeople)xml.Deserialize(stream);  
    }  
}
//snip error stuff
  • 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-06-02T15:35:32+00:00Added an answer on June 2, 2026 at 3:35 pm

    Have you tried using the XmlArray attribute?

    With your example it would be something like this:

    [XmlArray]
    [XmlArrayItem(ElementName="XmlPerson")]
    public List<XmlPerson> XmlPeople
    

    EDIT:

    Here, try the following structure:

    public struct XmlPerson
    {
        [XmlAttribute] public string Id   { get; set; }
        [XmlAttribute] public string Name { get; set; }
    }
    
    
    public class GroupOfPeople
    {
        [XmlArray]
        [XmlArrayItem(ElementName="XmlPerson")]
        public List<XmlPerson> XmlPeople { get; set; }
    }
    

    I don’t think it will be easy to add code to the Setter of the list, so what about getting that Dictionary when you actually need it?

    Like this:

    private Dictionary<string, string> _namesById;
    
    public Dictionary<string, string> NamesById
    {
        set { _namesById = value; }
        get
        {
            if (_namesById == null)
            {
                _namesById = new Dictionary<string, string>();
    
                foreach (var person in XmlPeople)
                {
                     _namesById.Add(person.Id, person.Name);
                }
            }
    
            return _namesById;
        }
    }
    

    This way you’ll get the items from the XML and will also mantain that Dictionary of yours.

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

Sidebar

Related Questions

I'm trying to get the basic of Treetop parsing. Here's a very simple bit
I'm trying to build a very simple bit of javascript that reads and displays
I am a little bit confused trying to implement a very simple mutex (lock)
What I'm trying to achieve is very simple, but a bit difficult to explain:
I'm trying a very simple demo of SignalR but getting the infamous undefined error
I'm trying something very simple. Upon page load the data in content div is
I am trying a very simple Fluent Nhibernate example: SQL 2005 database with one
I am trying a very simple override. I just want to add one line
I am trying to create very simple image browser with jQuery. I am doing
I have a very simple Mapping I'm trying to do. I'm following the NHibernate

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.