将XmlElement反序列化为自定义对象

时间:2013-03-05 23:07:19

标签: c# xml-serialization

我有一些XML需要反序列化为一个对象。大多数元素可以转换为字符串和整数,但是一个元素作为一个数字进入,我需要将其转换为三个布尔值。这是转换为布尔人的逻辑:

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;
        }

我尝试过像这个例子中那样创建隐式运算符,但我看到的唯一被调用的是我的无参数构造函数:

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

以下是要反序列化的属性:

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

我可以整天反序列化为整数,但由于某种原因,我无法将元素转换为自定义类型。这是我尝试从元素创建的类:

[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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可能必须使用IXmlSerializable界面来执行自定义反序列化

示例:

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;
        }
    }

我的测试:

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; }
    }
}

序列化:

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);
}

反序列化:

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

结果:

转换_value andd设置bool值

enter image description here