Solved

C# - XML serialization of an ENUM

Posted on 2013-06-18
2
581 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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