Search code examples
xmlparsingserializationhexuint

Deserialize xml string to uint property


Is it possible to deserialize a XML file with MyValue is 0x0001 and deserialize it to an uint property? What's the best way to implement this?

public class MyClass
{
    private string myValue;
    public uint MyValue
    {
         //checkValue method checks if myValue is a decimal or hex and number (returns an uint value).

         get { return checkValue(myValue); } 
         set { myValue = value.ToString(); }

    }
}

Solution

  • How about something like:

    public class MyClass
    {
        private uint? _myValue;
        [XmlIgnore]
        public uint MyValue
        {
            get{ return _myValue ?? checkValue(MyValueString); }
            set{ _myValue = value; }
        }    
    
        [XmlElement("MyValue")]
        public string MyValueString
        {
             //checkValue method checks if myValue is a decimal or hex and number (returns an uint value).
             get; set;
        }
    }