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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:21:25+00:00 2026-05-30T20:21:25+00:00

I have an array whose indices map back to Enum values. I would like

  • 0

I have an array whose indices map back to Enum values. I would like to use the C# Xml Serialization libraries to serialize this array. However, I would like each Xml node to say the name of the Enum type that it represents. How can I do this?

Example:

public class NumberOfEmployees
{
   public enum EmployeeType { Lawyer, Doctor, Engineer };

   public int[] NumEmployees { get; set; }

   // Constructor initializes array to size of "EmployeeType"
   public NumberOfEmployees()
   {
      int size = Enum.GetValues(typeof(EmployeeType)).Length;
      this.NumEmployees = new int[size];
   }    
}


Main()
{
   NumberOfEmployees numEmployees = new NumberOfEmployees();
   // Add doctors, lawyers, engineers, etc...
   // numEmployees.NumEmployees[(int)NumberOfEmployees.Lawyer] = 3;
   // numEmployees.NumEmployees[(int)NumberOfEmployees.Doctor] = 2;
   // numEmployees.NumEmployees[(int)NumberOfEmployees.Engineer] = 1;

   // Serialize to "file"
   FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
   XmlSerializer xml = new XmlSerializer(typeof(NumberOfEmployees));   
   xml.Serialize(fs, numEmployees);
   fs.Close();
}

The end result is xml that looks something like this:

<NumEmployees>
  <int>3</int>
  <int>2</int>
  <int>1</int>
</NumEmployees>

But what I want is:

<NumEmployees>
  <Lawyer>3</Lawyer>
  <Doctor>2</Doctor>
  <Engineer>1</Engineer>
</NumEmployees>

I can not store each number separately — it must be an array.

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-30T20:21:27+00:00Added an answer on May 30, 2026 at 8:21 pm

    You can implement the IXmlSerializable interface to completely customize serialization with XmlSerializer. Something similar to the following:

    public class NumberOfEmployees : IXmlSerializable
    {
        public int[] NumEmployees { get; set; }
    
        // Constructor initializes array to size of "EmployeeType"
        public NumberOfEmployees()
        {
            int size = Enum.GetValues(typeof(EmployeeType)).Length;
            this.NumEmployees = new int[size];
        }
    
        public XmlSchema GetSchema()
        {
            return null;
        }
    
        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement();
            NumEmployees[(int)EmployeeType.Lawyer] = int.Parse(reader.ReadElementString("Lawyer"));
            NumEmployees[(int)EmployeeType.Doctor] = int.Parse(reader.ReadElementString("Doctor"));
            NumEmployees[(int)EmployeeType.Engineer] = int.Parse(reader.ReadElementString("Engineer"));
            reader.ReadEndElement();
        }
    
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteElementString("Lawyer", NumEmployees[(int)EmployeeType.Lawyer].ToString());
            writer.WriteElementString("Doctor", NumEmployees[(int)EmployeeType.Doctor].ToString());
            writer.WriteElementString("Engineer", NumEmployees[(int)EmployeeType.Engineer].ToString());
        }
    }
    

    After you’ve done all that it might seem pointless to still use XmlSerializer since your class is handling all the work of serialization. It still makes sense, however, if the NumberOfEmployees is part of a larger XML structure (which I assume it is).

    Also note that the code doesn’t do any validation, because this is a simple example. So all three array elements will be expected to exist when the class is serialized, and all three XML elements will be expected to exist when it is deserialized.

    More info on the IXmlSerializable interface is available here:

    http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

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

Sidebar

Related Questions

I have a Numpy array and a list of indices whose values I would
i have a multidimensional array whose index/keys (not the values) are like this: this
I have an array of RGB values, whose size I can guarantee to match
I have an array of short whose values range between 0 and the maximum
I have an array whose structure is like $data = array{0=>'abc',1=>xyz,2=>tqs} Now I need
I have an array a whose values I wish to modify through another function.
I have an array of dates whose format is like this: 10/15/2005 or 2/10/2011.
I have an array whose values are all arrays of a specific format that
I have a problem initializing an array whose size is defined as extern const.
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>

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.