Solved

C# - XML serialization of an ENUM

Posted on 2013-06-18
2
584 Views
Last Modified: 2013-06-19
i have an enum like this

    enum Importance
    {
      None=0,
      Trivial=1,
      Regular=2,
      Important=3,
      Critica=4
    };

I will like to XML serialize it and be able to read from a file . So I can add and modify the enum out side the application.  How will I specify the XML attributes in the enum, and what function(s) I need to call to retrival enum form file?
0
Comment
Question by:tommym121
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39258480
So I can add and modify the enum out side the application.
An enum is a static definition of code. What you are talking about is dynamic, and you won't be able to do this without reflection, and quite likely dynamic compilation. It's not incredibly difficult to do, but I would question the benefit of doing such.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39258486
there's no problem serializing enum properties as xml attribute, simply decorate the property with  [XmlAttribute]:
    public class Test
    {
        public enum Importance
        {
            None = 0,
            Trivial = 1,
            Regular = 2,
            Important = 3,
            Critica = 4
        };
 [XmlAttribute]
        public Importance importance { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();
            using (var writer = XmlWriter.Create(sb))
            {
                var test = new Test
                {
                    importance = Test.Importance.Regular
                };
                var serializer = new XmlSerializer(test.GetType());
                serializer.Serialize(writer, test);
            }
            Console.WriteLine(sb.ToString());
        }
    }

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question