一个类[Serializable] Currency
有两个字段和XmlElement
反射属性:
[XmlElement("currencyname")]
CurrencyValue m_Value { get; }
[XmlElement("exchangerate")]
public decimal exchangeRate { get; set; }
CurrencyValue
是Currency
类外的枚举。
我尝试使用[XmlEnum("...")]
属性,也尝试在类中“拉”设置枚举值,使用
[XmlElement("Value")]
public CurrencyValue m_value
{
get { return m_value.ToString(); }
}
但无济于事。类方法ToXML()
如下所示:
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();
}
}
}
我的问题是:像这样的枚举可以包含在我的对象的序列化中吗?我看不出有什么理由不将它包含在类本身中,但是这个枚举有可能在其他地方使用并且改变它的位置对我有利不是一种选择。