Amzath
Amzath

Reputation: 3249

Serializing Enum type emits different element name

Working on XML serialization. When serializing enum type, xml element name of it to be changed. Any help would be appreciated.

I am using .Net xml serialization to serialize this object and that produces the below xml output

<MyEnums>   <MyEnum>One</MyEnum>    <MyEnum>Two</MyEnum>    <MyEnum>Three</MyEnum></MyEnums>

Here is the class

public class Request
{
        public List<MyEnum> MyEnums { get; set; }
}

Here is the XML I wanted to emit

<MyEnums>
<ModifiedElementName>One</ModifiedElementName>
<ModifiedElementName>Two</ModifiedElementName>
<ModifiedElementName>Three</ModifiedElementName>

Here is Enum Type

enum MyEnum
{
    One,
    Two,
    Three
}

Expecting output be as xml

<ModifiedElementName>One</ModifiedElementName>

Upvotes: 0

Views: 730

Answers (2)

carlosfigueira
carlosfigueira

Reputation: 87258

You can play with the [XmlElement], [XmlArray] and [XmlArrayItem] attributes to get what you want. The example below uses the last two to get to what I think you need, but feel free to play with the others to get exactly the XML you want output.

    public class Request
    {
        [XmlArray(ElementName = "MyEnums")]
        [XmlArrayItem(ElementName = "ModifiedElementName")]
        public List<MyEnum> MyEnums { get; set; }
    }

    public enum MyEnum
    {
        One,
        Two,
        Three
    }

    public static void Test()
    {
        XmlSerializer xs = new XmlSerializer(typeof(Request));
        MemoryStream ms = new MemoryStream();
        Request req = new Request
        { 
            MyEnums = new List<MyEnum>
            {
                MyEnum.One,
                MyEnum.Two,
                MyEnum.Three
            }
        };
        xs.Serialize(ms, req);
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
    }

Upvotes: 1

user743382
user743382

Reputation:

You can use the XmlType attribute on your enum, like so:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace Scratch
{
    [XmlType("ModifiedEnumName")]
    public enum TestEnum
    {
        One,
        Two,
        Three,
    }

    public class TestClass
    {
        public TestClass()
        {
            MyEnums = new List<TestEnum>();
        }

        public List<TestEnum> MyEnums { get; set; }
    }

    static class Program
    {
        static void Main(string[] args)
        {
            using (var sw = new StringWriter())
            {
                new XmlSerializer(typeof(TestClass)).Serialize(sw, new TestClass { MyEnums = { TestEnum.Two } });
                Console.WriteLine(sw.GetStringBuilder());
            }
        }
    }
}

This outputs

<?xml version="1.0" encoding="utf-16"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyEnums>
    <ModifiedEnumName>Two</ModifiedEnumName>
  </MyEnums>
</TestClass>

Edit: this assumes all TestEnums need to be serialised the same way. If you wish to be able to specify different XML element names for different properties of the same type, go with carlosfigueira's answer.

Upvotes: 5

Related Questions