0

我必须在 XML 下反序列化。但我必须将传感器数据存储到动态加载的类中。我怎样才能做到这一点?

XML 格式

   <Root>
        <Sensor id="0" Channel="1000">
            <SensorName>RM-T</SensorName>
            <SensorValue>148.00</SensorValue>
        </Sensor>
        <Sensor id="1" Channel="1001">
            <SensorName>SAT1</SensorName>
            <SensorValue>148.00</SensorValue>
        </Sensor>
    </Root>

存储反序列化数据的主类

[Serializable]
[XmlRoot(ElementName = "Root")]
public class Controller
{        
    public List<Sensor> SensorList
    {
        get;
        set;
    }       
}

传感器类

   public abstract class Sensor
    {
        String SensorName { get; set; }
        String SensorValue { get; set; }   
        public abstract override String ToString();     
    }

常规传感器类

    public class RegularSensor : Sensor
    {

        public override string ToString()
        {
           //Implementation goes here
        }
    }

在反序列化期间,我需要将传感器数据加载到 RegularSensor 类我该怎么做?

4

1 回答 1

-1

这适合你吗:

 public class RegularSensor : Sensor
    {
        Sensor mySensor = new Sernsor();
        public override string ToString()
        {
           //Implementation goes here
        }
    }

然后在函数中反序列化这个对象'mySensor'。你可以获得所有数据。

于 2012-12-31T06:43:05.887 回答