1

A class [Serializable] Currency has two fields and XmlElement reflection attributes:

[XmlElement("currencyname")]
CurrencyValue m_Value { get; }

[XmlElement("exchangerate")]
public decimal exchangeRate { get; set; }

CurrencyValue is an enum that is outside the Currency class.

I've tried to use [XmlEnum("...")] attributes and have also attempted to "pull" the set enum value inside of the class, using

[XmlElement("Value")]
public CurrencyValue m_value
{
    get { return m_value.ToString(); }
}

but to no avail. The class method ToXML() looks like this:

public string ToXML(bool indent = false, bool omitXmlDeclaration = true, bool includeDefaultNamespaces = false)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Currency));
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

    if (!includeDefaultNamespaces)
    {
        ns.Add("", "");
    }

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = new UnicodeEncoding(false, false);
    settings.Indent = indent;
    settings.OmitXmlDeclaration = omitXmlDeclaration;

    using (StringWriter stringWriter = new StringWriter())
    {
        using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
        {
            serializer.Serialize(xmlWriter, this, ns);
            return stringWriter.ToString();
        }
    }
}

My question is: can an enum like that be included in the serialization of my object? I wouldn't see a reason to not include it inside the class itself, but it's possible that this enum is used elsewhere and changing it's location in my favor is not an option.

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
  • Why do you feel the need to include the enum in the serialized XML? – Oded Sep 27 '12 at 14:57
  • I'm comparing the enum to a set of currencies being pulled in from either a database or a service (db at this point, might change) I'm not personally partial to including the enum, but I was asked to do so for string comparison reasons... – Wim Ombelets Sep 27 '12 at 14:59
  • That doesn't explain why you need a copy in the XML. So long as the value of the enum field is stored correctly, why do you need the whole thing? – Oded Sep 27 '12 at 15:01
  • What do you mean in this context with "correctly stored"? – Wim Ombelets Sep 27 '12 at 15:15
  • The underlying value of the enum will be stored. This will get deserialized to the correct enum value. – Oded Sep 27 '12 at 15:16
  • that value is correctly present in the object, but I can't get it to serialize. The field name serializes, the value does not even though it's all there in the objects. – Wim Ombelets Sep 27 '12 at 15:27
  • possible duplicate of [Why isn't my public property serialized by the XmlSerializer?](http://stackoverflow.com/questions/575432/why-isnt-my-public-property-serialized-by-the-xmlserializer) – nitzmahone Sep 27 '12 at 17:30

1 Answers1

1

Sadly, XmlSerializer will ignore members without a public setter. Add a setter (even one that just throws, if you don't want it used) and ensure that the property is public, and it should work fine. You shouldn't even need the XmlEnum attribute.

nitzmahone
  • 13,720
  • 2
  • 36
  • 39
  • See also: http://stackoverflow.com/questions/575432/why-isnt-my-public-property-serialized-by-the-xmlserializer – nitzmahone Sep 27 '12 at 17:30