0

I have a list of objects which belong to the custom defined class "Device", which I have serialized into an XML file. The class "Device" has a nested class called "Function".

<ArrayOfDevice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Device>
    <Name>CellaTemp PA</Name>
<Type>DeviceX</Type>
<Station>2</Station>
<Function>
  <Name>Function1</Name>
</Function>
  </Device>
  <Device>
    <Name>EKU1 KR</Name>
    <Type>DeviceY</Type>
    <Station>1</Station>
    <Function>
  <Name>Function2</Name>
</Function>

Now I want to open this XML file "/.../DevicesXML" in a new C# solution and deserialize it into a list consisting of objects of the "Device" class.

How do I load the file correctly? Which commands and "using".namespaces do I need?

Do I have to define the classes in the new solution like in the original one?

The class "Device" has been defined as follows:

[Serializable()]
public class Device
{
    public string Name { get; set; }
    public string Type { get; set; }
    public int Station { get; set; }
    public Function func{ get; set; }

    [Serializable()]
    public class Function
    {
        public string Name { get; set; }

        public Function() { }
        public Function(string name = "No Name")
        {
            Name = name;
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Name", Name);
        }

        public Function(SerializationInfo info, StreamingContext context)
        {
            Name = (string)info.GetValue("Name", typeof(string));
        }
    }
        public Device() { }
    public Device(string name = "No Name", string type = "No type", int station = 0, 
        string sname="No Name", string sfeat="Nothing")
    {
        Name = name;
        Type = type;
        Station = station;
        Func1= new Function(sname, sfeat);
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Name", Name);
        info.AddValue("Type", Type);
        info.AddValue("Station", Station);
        info.AddValue("Func1", Func1);
    }

    public Device(SerializationInfo info, StreamingContext context)
    {
        Name = (string)info.GetValue("Name", typeof(string));
        Type = (string)info.GetValue("Type", typeof(string));
        Station = (int)info.GetValue("Station", typeof(double));
        Func1= (Function)info.GetValue("Func1", typeof(Function));
    }
}    

1 Answers1

0

You could use Xml.Serialization

Here is the minimal working sample:

namespace ConsoleApp1
{
    [Serializable]
    public class Device
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public int Station { get; set; }
        public Function Function { get; set; }
    }

    [Serializable]
    public class Function
    {
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var file = "<some_path>";
            using (var reader = new StreamReader(file))
            {
                var devices = (List<Device>)new XmlSerializer(typeof(List<Device>)).Deserialize(reader);

                // do something with devices.
            }
        }
    }
}
EylM
  • 5,967
  • 2
  • 16
  • 28
  • Do the classes need to be serializable? – KARA BOĞA Jul 09 '19 at 18:09
  • No. "Unless you intend to serialize your class using Binary or Soap, do not bother marking your class as [Serializable]. XML and JSON serializers are not even aware of its existence." https://stackoverflow.com/a/50991360/1410246 – EylM Jul 09 '19 at 18:14