[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C# - XML serialization of an ENUM

Posted on 2013-06-18
2
Medium Priority
?
614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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:Meir Rivkin
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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