using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; namespace SCMA { /// /// Classe per serializzazione / deserializzazione del datamodel x le informazioni necessarie x ADAPTER OPC-UA 2.1+ /// /// ref: /// - https://stackoverflow.com/questions/37198689/deserialization-of-complex-object-c-sharp /// - https://stackoverflow.com/questions/15907357/deserialization-of-xml-file-by-using-xmlarray /// - http://www.cambiaresearch.com/articles/33/how-can-i-easily-manage-an-xml-configuration-file-in-dotnet /// [Serializable] [XmlRoot("ModelDesign")] public class DataModelXML { // rivedere con // - https://docs.microsoft.com/it-it/dotnet/standard/serialization/controlling-xml-serialization-using-attributes // - https://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm // RIPENSARE OBJ GERARCHICO IN TOTO... // ModelDesign > MAchine > List + List + List ognuno coi suoi attributi... [XmlArray("Machine")] [XmlArrayItem("Property", Type = typeof(Property))] [XmlArrayItem("Variable", Type = typeof(Variable))] [XmlArrayItem("Component", Type = typeof(Component))] public ouData[] PVData { get; set; } //[XmlArray("Machine")] //[XmlArrayItem("Property")] //[XmlArrayItem("Property", Type = typeof(Property))] //[XmlArrayItem("Variable", Type = typeof(Variable))] //[XmlArrayItem("Component", Type = typeof(Component))] //public cData[] CData { get; set; } /// /// Serializzazione XML dell'oggetto conf dell'adapter /// /// /// public static void Serialize(string file, DataModelXML c) { // prima provo a creare il file vuoto... if (!File.Exists(file)) { string dirPath = file.Substring(0, file.LastIndexOf('\\')); // verifico directory if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } } // salvo effettivamente file... XmlSerializer xs = new XmlSerializer(c.GetType()); StreamWriter writer = File.CreateText(file); xs.Serialize(writer, c); writer.Flush(); writer.Close(); } /// /// deserializzazione oggetto conf adapter /// /// /// public static DataModelXML Deserialize(string file) { XmlSerializer xs = new XmlSerializer(typeof(DataModelXML)); StreamReader reader = File.OpenText(file); DataModelXML c = (DataModelXML)xs.Deserialize(reader); reader.Close(); return c; } /// /// restitusice forma XML grezza del file /// /// /// public static string rawXml(string file) { string answ = ""; XmlSerializer xs = new XmlSerializer(typeof(DataModelXML)); StreamReader reader = File.OpenText(file); answ = reader.ReadToEnd(); reader.Close(); return answ; } } [Serializable] public class Modules { [XmlElement] public Channel channel { get; set; } [XmlArray("Machine")] //[XmlArrayItem("Property")] //[XmlArrayItem("Property", Type = typeof(Property))] //[XmlArrayItem("Variable", Type = typeof(Variable))] //[XmlArrayItem("Component", Type = typeof(Component))] public ouData[] PVData { get; set; } //public List Properties { get; set; } } [Serializable] [XmlInclude(typeof(Property))] [XmlInclude(typeof(Variable))] [XmlInclude(typeof(Component))] public class ouData { [XmlAttribute] public string Name { get; set; } [XmlAttribute] public string SymbolicName { get; set; } [XmlAttribute] public string BrowseName { get; set; } [XmlAttribute] public string DataType { get; set; } [XmlAttribute] public string ValueRank { get; set; } [XmlAttribute] public string Value { get; set; } [XmlAttribute] public string Units { get; set; } } [Serializable] public class Property : ouData { } [Serializable] public class Variable : ouData { } [Serializable] public class Component : ouData { } [Serializable] public class Channel { [XmlArray("resources")] [XmlArrayItem("resource")] public List resources { get; set; } } [Serializable] public class Resources { [XmlAttribute] public string name { get; set; } [XmlAttribute] public string url { get; set; } [XmlAttribute] public string refresh_interval { get; set; } [XmlText] public string someText { get; set; } } }