0

I'm trying to deserialize a piece of XML offered by some API. However this API is dumb, for example, a bool is not true, but True. And having an element

[XmlElement("Foo")]
public bool Foo { get;set; }

and then the matching XML:

<...><Foo>True</Foo></...>

does NOT work, because True is not a valid representation of bool (the API is written in Python, which is, I think, more forgiving).

Is there any way to put some attribute on my property Foo to say to the system: when you encounter this element, put it through this converter class?

Edit: The XML is large, and most of them are stupid, not directly convertible objects, like 234KB, which I need to parse to the exact value.

Do I need to write a wrapper property for each of them?

4

1 回答 1

1

您可以使用支持属性:

public class MyModel
{
    [XmlIgnore]
    public bool Foo 
    {
        get
        {
            return string.Equals(FooXml, "true", StringComparison.OrdinalIgnoreCase);
        }
        set
        {
            FooXml = value.ToString();
        }
    }

    [XmlElement("Foo")]
    public string FooXml { get; set; }
}
于 2013-05-11T18:18:57.507 回答