2

我有一些需要反序列化为对象的 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;
        }
    }
}
4

1 回答 1

1

您可能必须使用该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>

测试类:

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

结果:

转换_valueandd 设置 bool 值

在此处输入图像描述

于 2013-03-06T00:47:05.410 回答