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.

7
  • Why do you feel the need to include the enum in the serialized XML? Commented Sep 27, 2012 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... Commented Sep 27, 2012 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? Commented Sep 27, 2012 at 15:01
  • What do you mean in this context with "correctly stored"? Commented Sep 27, 2012 at 15:15
  • The underlying value of the enum will be stored. This will get deserialized to the correct enum value. Commented Sep 27, 2012 at 15:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.