0

I need to deserialize/serialize XML which looks like this:

<color>
    <green/>
</color>

where <green/> may be <red/>, <blue/> etc. - very large (but limited) set.

I'd like to describe it as simple enum in my code:

enum ColorName
{
    [XmlEnum("red")]
    Red,
    [XmlEnum("green")]
    Green,
    [XmlEnum("blue")]
    Blue,

    ...
    etc.
}

But, if I write my object model like this:

class Color
{
    [XmlElement("name")]
    public ColorName ColorName;
}

class Something
{
    [XmlElement("color")]
    public Color Color;
}

enum gets into XML as a value, rather than element name:

<color>
    <name>green</name>
</color>

Is there any way to get enum value written into XML element name (see the first XML snippet - that's the goal), rather than XML element value, without having to re-type all the values (it's a very large set) as empty class names, or resorting to custom serialization (I would like to avoid it, because serialized class contains a lot of other members, which are perfectly serialized by default)?

(I can't change the schema, it's third-party).

4

1 に答える 1

0

いいえ、これは単にクラスを[Xml*]属性で装飾するだけではサポートされていません。自分で実装IXmlSerializableSomethingて実行する必要があります。GetSchemaほとんどの場合、メソッドを気にする必要はないことに注意してください。実装ReadXmlしてWriteXml問題ありません。

于 2013-11-14T20:04:44.153 に答える