pmichelbrink
pmichelbrink

Reputation: 37

Deserializing XmlElement as Custom Object

I have some XML that I need to deserialize into an object. Most of the elements can be translated into strings and integers, but one element is coming in as a number that I need to translate into three Boolean values. This is the logic behind the conversion to Booleans:

switch (_valFromXML)
        {
            case 0:
                _bool1= true;
                break;
            case 1:
                _bool2= true;
                break;
            case 2:
                _bool1= true;
                _bool2= true;
                break;
            case 3:
                _bool3= true;
                break;
            case 4:
                _bool1= true;
                _bool3= true;
                break;
            case 5:
                _bool2= true;
                _bool3= true;
                break;
            case 6:
                _bool1= true;
                _bool2= true;
                _bool3= true;
                break;
        }

I have tried creating implicit operators like in this example, but the only thing I see being called is my parameterless constructor:

http://forums.asp.net/t/1187054.aspx

Here is the property being deserialized:

private ConvertElement _convertElement;
[XmlElement("ConvertElement", typeof(ConvertElement))]
public ConvertElement ConvertElement
{
    get { return _convertElement; }
    set { _convertElement= value; }
}

I can deserialize into an integer all day long, but for some reason I can't get the element converted to a custom type. Here is the class I am trying to create from the element:

[Serializable]
public class ConvertElement
{
     int _value;
     bool _bool1;
     bool _bool2;
     bool _bool3;

    public static implicit operator int(ConvertElement x)
    {
        return x.Value;
    }
    public static implicit operator ConvertElement(int value)
    {
        return new ConvertElement(value);
    }

    public int Value
    {
        get { return _value; }
        set { _value = value; }
    } 

    public ConvertElement()
    {
    }

    public DocSelection(int value) 
    {
        _value= value;
        switch (_value)
        {
            case 0:
                _bool1= true;
                break;
            case 1:
                _bool2= true;
                break;
            case 2:
                _bool1= true;
                _bool2= true;
                break;
            case 3:
                _bool3= true;
                break;
            case 4:
                _bool1= true;
                _bool3= true;
                break;
            case 5:
                _bool2= true;
                _bool3= true;
                break;
            case 6:
                _bool1= true;
                _bool2= true;
                _bool3= true;
                break;
        }
    }
}

Upvotes: 2

Views: 3744

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43616

You may have to use the IXmlSerializable interface to perform custom deserialization

Example:

public class ConvertElement : IXmlSerializable
{

    private int _value;
    private bool _bool1;
    private bool _bool2;
    private bool _bool3;

    public ConvertElement()
    {
    }

    #region IXmlSerializable implementation

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteValue(_value);
    }

    public void ReadXml(XmlReader reader)
    {
        _value = int.Parse(reader.ReadString());
        DocSelection(_value);
    }

    public XmlSchema GetSchema()
    {
        return null;
    }

    #endregion

    public void DocSelection(int value)
    {
        _value = value;
        switch (_value)
        {
            case 0:
                _bool1 = true;
                break;
            case 1:
                _bool2 = true;
                break;
            case 2:
                _bool1 = true;
                _bool2 = true;
                break;
            case 3:
                _bool3 = true;
                break;
            case 4:
                _bool1 = true;
                _bool3 = true;
                break;
            case 5:
                _bool2 = true;
                _bool3 = true;
                break;
            case 6:
                _bool1 = true;
                _bool2 = true;
                _bool3 = true;
                break;
        }
    }

My Test:

Xml:

<?xml version="1.0"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ConvertElement>6</ConvertElement>
</TestClass>

TestClass:

public class TestClass
{
    private ConvertElement _convertElement;
    [XmlElement("ConvertElement", typeof(ConvertElement))]
    public ConvertElement ConvertElement
    {
        get { return _convertElement; }
        set { _convertElement = value; }
    }
}

Serialize:

TestClass test = new TestClass
{
    ConvertElement = new ConvertElement()
};

XmlSerializer xmlSerializer = new XmlSerializer(typeof(TestClass));
using (FileStream stream = new FileStream("c:\\ConvertElement.xml", FileMode.OpenOrCreate))
{
    xmlSerializer.Serialize(stream, test);
}

Deserialize:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(TestClass));
using (FileStream stream = new FileStream("c:\\ConvertElement.xml", FileMode.Open))
{
  var result = xmlSerializer.Deserialize(stream);
}

Result:

converts the _value andd sets the bool values

enter image description here

Upvotes: 1

Related Questions