public static List <XMLSensorObject> GetSensorItemsFromXML() { List <XMLSensorObject> lst = new List <XMLSensorObject>(); ArrayList items = new ArrayList(); items.Clear(); LoadXML(); foreach (XmlNode xmln in xDoc.SelectNodes("/root/Sensoren")) { foreach (XmlNode child in xmln.ChildNodes) { XMLSensorObject tmpSensor = XmlReadChildItem(child); lst.Add(tmpSensor); Console.WriteLine("******************************************************************"); Console.WriteLine("Read Sensor item ..."); Console.WriteLine("Sensor.Name == {0}", tmpSensor.Name); Console.WriteLine("Sensor.Active == {0}", (tmpSensor.Active) ? "Y" : "N"); Console.WriteLine("Sensor.Protocol == {0}", Enum.GetName(typeof(XMLProtocol), tmpSensor.DataInterfaceType)); if (tmpSensor.DataInterfaceType == XMLProtocol.HTTP) { Console.WriteLine("Sensor.URL == {0}", tmpSensor.URL); } else if (tmpSensor.DataInterfaceType == XMLProtocol.COM) { Console.WriteLine("Sensor.Port == {0}", tmpSensor.Port); Console.WriteLine("Sensor.Baudrate == {0}", tmpSensor.Baudrate.ToString()); Console.WriteLine("Sensor.DtrEnabled == {0}", (tmpSensor.DtrEnabled) ? "Y" : "N"); } Console.WriteLine("Sensor.writeToDB == {0}", (tmpSensor.WriteToDatabase) ? "Y" : "N"); Console.WriteLine("Sensor.LogEnabled == {0}", (tmpSensor.LogEnabled) ? "Y" : "N"); Console.WriteLine("Sensor.LogFilePath == {0}", tmpSensor.LogFilePath); Console.WriteLine("Sensor.HTMLEnabled == {0}", (tmpSensor.HTMLEnabled) ? "Y" : "N"); Console.WriteLine("Sensor.numLogEntries == {0}", tmpSensor.NumLogEntries.ToString()); Console.WriteLine("Sensor item was read successfully!"); Console.WriteLine("******************************************************************"); } } return(lst); }
public static XMLSensorObject XmlReadChildItem(XmlNode child) { XMLSensorObject tmpSensor = new XMLSensorObject(); ArrayList items = new ArrayList(); items.Clear(); tmpSensor.Name = GetInnerText(child, "Bezeichnung"); //Name = Unique Identifier, not allowed to exist more than one time if (items.IndexOf(tmpSensor.Name) >= 0) { throw new Exception("Fehler XML: Die Bezeichnung muss für jeden Sensor eindeutig sein.\nDer Name '" + tmpSensor.Name + "' existiert bereits!"); } else { items.Add(tmpSensor.Name); } tmpSensor.Active = CheckBool(GetInnerText(child, "Aktiv")); tmpSensor.LogEnabled = CheckBool(GetInnerText(child, "LogEnabled")); tmpSensor.HTMLEnabled = CheckBool(GetInnerText(child, "WriteHTML")); tmpSensor.WriteToDatabase = CheckBool(GetInnerText(child, "WriteToDatabase")); tmpSensor.NumLogEntries = 50; //default int numEntries; if (int.TryParse(GetInnerText(child, "NumLogItems"), out numEntries)) { if (numEntries >= 1 && numEntries <= 500000) { tmpSensor.NumLogEntries = numEntries; } else { throw new Exception("XML Fehler: Die Anzahl der Num Einträge muss zwischen 1 und 500000 liegen!"); } } else { throw new Exception("XML Fehler: Die Anzahl der Num Einträge kann nicht verarbeitet werden!"); } long maxLogFileSize; if (long.TryParse(GetInnerText(child, "maxLogFileSize"), out maxLogFileSize)) { if (maxLogFileSize >= 1048576 && maxLogFileSize <= 1073741824) { tmpSensor.MaxLogFileSize = maxLogFileSize; } else { throw new Exception("XML Fehler: Die maximale Größe des Logfiles muss zwischen 1.048.576 und 1.073.741.824 liegen!"); } } else { throw new Exception("XML Fehler: Die maximale Größe des Logfiles kann nicht verarbeitet werden!"); } tmpSensor.MaxLogFileSize = maxLogFileSize; tmpSensor.DataInterfaceType = GetProtocol(GetInnerText(child, "Protocol")); if (tmpSensor.DataInterfaceType == XMLProtocol.COM) { tmpSensor.DtrEnabled = CheckBool(GetInnerText(child, "COM/DtrEnabled")); tmpSensor.Port = GetInnerText(child, "COM/Port"); int Baudrate; if (int.TryParse(GetInnerText(child, "COM/Baudrate"), out Baudrate)) { if (Baudrate >= 8) { tmpSensor.Baudrate = Baudrate; } else { throw new Exception("XML Fehler: Baudrate hat einen Wert kleiner 8"); } } else { tmpSensor.Baudrate = 115200; } } else if (tmpSensor.DataInterfaceType == XMLProtocol.HTTP) { tmpSensor.URL = GetInnerText(child, "HTTP/URL"); } else { throw new Exception("XML Fehler: Das Protokoll muss entweder 'HTTP' oder 'COM' sein (Sensor '" + tmpSensor.Name + "')."); } tmpSensor.LogFilePath = GetInnerText(child, "LogFile"); return(tmpSensor); }