Solved

C# - XML serialization of an ENUM

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now