0

I have to send string with xml containing enums serialized to xml. Those enums are in nuget package, so I can't edit them. All enums have name, value and description attribute.

public enum FileExtenstion
    [Description("XML extension")]
    xml = 1,
    
    [Description("TXT extension")]
    txt = 2
end enum

My function to get xml from this enum looks like this:

public static string GetFileExtension()
{
    var _cache = System.Enum.GetValues(typeof(FileExtension))
        .Cast<FileExtension>()
        .Select(enumValue => new EnumToXmlSerializer
        {
            Name = enumValue.ToString(),
            Value = (int)enumValue,
            Description = enumValue.GetDescription()
        }).ToList();

    (...)
}

public class EnumToXmlSerializer
{
    public string Name { get; set; }
    public int Value { get; set; }
    public string Description { get; set; }
}

Works well. But I have a lot of enums to convert and better way will be to prepare some parametrized function with convert string with name of enum ("FileExtension") to type. I haven't idea how to convert enum name (not enum every value name but enum type name) and convert its to type. In stackoverflow I found only conversion for enum values not all types.

I'm looking for solution to prepare function like this:

public string GetXmlFromEnumTypeName("FileExtension")

to get:

<enums>
<enum><name>xml</name><value>1</value><description>XML extension</description></enum>
<enum><name>txt</name><value>2</value><description>TXT extension</description></enum>
</enums>

I suppose there will be one line to change my GetFileExtension function but I usually get it wrong about things like that.

deku
  • 85
  • 10
  • You didn't post any XML serialization code. Both the XmlSerializer and DataContractSerializer classes handle enums just fine using standardized XML Schema forms. What you posted has nothing to do with how enums are serialized in XML, it's a completely custom form – Panagiotis Kanavos Jul 22 '22 at 10:14
  • `Best` by almost any definition is to actually follow the XML Schema standards instead of inventing a new format. Every other language, library and application will be able to handle the XML file given its schema. You can specify enums in an XSD document [as this question shows](https://stackoverflow.com/questions/6293400/mapping-xml-values-to-enum-type). The `XmlEnum` attribute can be used to control the generated values – Panagiotis Kanavos Jul 22 '22 at 10:17
  • You should check this answer here. Maybe can help. https://stackoverflow.com/questions/2306299/how-do-you-use-xmlserialize-for-enum-typed-properties-in-c – Arthur Santana Jul 22 '22 at 10:18
  • @PanagiotisKanavos, maybe I should't name it as "serialization". Better name is a list conversion. `_cache` is a list of `EnumToXmlSerializer` objects, but I think its obvious for you. Xml code is general example. I tought the `_cache` line will be most important topic of discussion. – deku Jul 22 '22 at 11:05
  • @ArthurSantana, As I said in first paragraph: I can't extra decorate enums (they became from nuget packages). – deku Jul 22 '22 at 11:08
  • Your question is asking one thing but you say you want another. Edit the title and text to ask what you really want. Serialize the generated objects, not the Enums. Where is the serialization code? What is the problem with it? – Panagiotis Kanavos Jul 22 '22 at 11:09
  • You are absolutely right - I changed. Thanks. – deku Jul 22 '22 at 11:12
  • So what's the actual problem? How to find all the enums in that assembly? Or do you have a list of enum names you want to use? – Panagiotis Kanavos Jul 22 '22 at 11:14
  • Are you looking for [Assembly.GetType(string)](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.gettype?view=net-6.0) perhaps? – Panagiotis Kanavos Jul 22 '22 at 11:15
  • I'll check it, but in first test I see that Assebly.GetType doesn't get types from nuget packages. – deku Jul 25 '22 at 05:34

0 Answers0