0

I have to deserialize below XML.But i have to store Sensor data to a dynamically loaded class. How can i do that?

XML format

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

Main class to store deserialized data

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

Sensor Class

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

RegularSensor Class

    public class RegularSensor : Sensor
    {

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

During deserialization i need to load sensor data to RegularSensor class How can i do that?

Prasanth V J
  • 1,126
  • 14
  • 32
  • Why does it need to be dynamic? Looks pretty static to me.. – Simon Whitehead Dec 31 '12 at 06:27
  • The XML is from a hardware a controller depending upon the controller the data handling will change.So there will be different implementation for Sensorvalue. In future there will be different function in the abstact class with different implementation – Prasanth V J Dec 31 '12 at 06:34
  • ...but SensorValue is an attribute in your example.. not the root object. Are you saying the root object (structural layout of the xml) will change? Or just values? – Simon Whitehead Dec 31 '12 at 06:41
  • Structure the XMl will not change.But alias name of the nodes may change.For eg in some XML 'SensorValue' will be 'Value' – Prasanth V J Dec 31 '12 at 08:25
  • In java i did the same using this code - XStream xStreamXmlSerializer = new XStream(new DomDriver()); xStreamXmlSerializer.alias("Sensor", RegularSensor.class); – Prasanth V J Dec 31 '12 at 08:28
  • Check below link [http://stackoverflow.com/questions/14108509/deserialize-a-class-with-interface][1] [1]: http://stackoverflow.com/questions/14108509/deserialize-a-class-with-interface – Prasanth V J Nov 11 '14 at 05:06

1 Answers1

-1

Is this ok for you :

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

and then deserialize this object 'mySensor' in a function.And you can get all datas.

king
  • 304
  • 1
  • 8