Autor Beitrag
HoPPeL
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Mo 15.08.11 11:03 
Hallo,

habe die aufgabe, daten in meiner form über eine logik zu überprüfen.
Also wird der der wert überschritten, gibts nen alarm. Und diese Schwellwerte sollen relativ einfach in einer xml datei auch für Laien änderbar sein. So meine Frage ist jetzt, ob das so praktikabel ist, oder gibts besserere Alternativen?
und wie stelle ich das mit der xml datei an, hat da jemand ne gute anleitung? weil plan habe ich bis dahin 0 davon
dark-destination1988
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 178
Erhaltene Danke: 21



BeitragVerfasst: Mo 15.08.11 12:01 
xml es ist definitiv keine schierige Aufgabe. sollen die werte den bei der Eingabe überprüft werden, oder beispielsweise beim drücken eines buttons?
du legst eine Klasse an mit den properties SchwelleOben und SchwelleUnten (zum Beispiel als double) über die klasse [Serializable]
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
//wenn nicht vorhanden neue anlegen
if(File.Exists(@Application.StartupPath + "/deinekonfi.xml")==false)
{
       //wenn nicht vorhanden --> Default Einstellungen erstellen
       ConfigClass newConf = new ConfigClass();
       newConf.SchwelleOben=10;
       newConf.SchwelleUnten=5;
       XmlSerializer ser = new XmlSerializer(typeof(ConfigClass));
       FileStream str = new FileStream(@Application.StartupPath + "/deinekonfi.xml", FileMode.Create);
       ser.Serialize(str, newConf);
       str.Close(); 
}
//deserialisieren
XmlSerializer deser = new XmlSerializer(typeof(ConfigClass));
StreamReader sr = new StreamReader(@Application.StartupPath + "/deinekonfi.xml");
_config = (ConfigClass)deser.Deserialize(sr);
HoPPeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Mo 15.08.11 13:13 
naja per event kommen ständig daten rein, z.B. ne Temperatur. Steigt die nun über 23°C solls halt einen alarm geben :)
dark-destination1988
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 178
Erhaltene Danke: 21



BeitragVerfasst: Mo 15.08.11 13:15 
naja dann überprüfst du im eventhandler die temp mit der des konfigfiles, kann den das konfigfile auch während des betriebs geändert werden? wenn ja, dann musst du bei jedem mal die konfig neu auslesen, ansonsten reicht ja einmal beim programmstart
HoPPeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Mi 17.08.11 11:21 
Danke schonmal für die Hilfe, habe auch nochmal bissle geguckt und mir jetzt was kleines zum testen gebastelt:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
{
    public class Configuration
    {
        int _warning;
        int _alert;

       public Configuration()
        {
         //   _warning = 50;
         //   _alert = 100;
        }

        public static void Serialize(string file, Configuration c)
        {
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(c.GetType());
            StreamWriter writer = File.CreateText(file);
            xs.Serialize(writer, c);
            writer.Flush();
            writer.Close();
        }
        public static Configuration Deserialize(string file)
        {
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(Configuration));
            StreamReader reader = File.OpenText(file);
            Configuration cfg = (Configuration)xs.Deserialize(reader);
            reader.Close();
            return cfg;
        }
        public int warning
        {
            get { return _warning; }
            set { _warning = value; }
        }
        public int alert
        {
            get { return _alert; }
            set { _alert = value; }
        }
    }

}


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

         // Read the configuration object from a file
            Configuration cfg = Configuration.Deserialize("configuration.xml");

         // Write out the variables read from the file
         
         textBox1.Text= cfg.alert.ToString();
         textBox1.Text+=  cfg.warning.ToString();

        }
    }


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
<?xml version="1.0"?>
<Sensors>
  <ID1>
    <warning>70</warning>
    <alert>100</alert>
  </ID1>
  <ID2>
    <warning>80</warning>
    <alert>110</alert>
  </ID2>
</Sensors>


aber so richtig will das nicht funktionieren... bekomme immer nur 00 ausgegeben :(
Ein offensichtlicher Fehler ist auf jeden fall, dass ich 2 warning Werte in meiner xml Datei habe... weiß aber nicht, wie ich das schreiben muss im C# Code um sauber nach ID1 und ID2 zu trennen?!

mfg
dark-destination1988
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 178
Erhaltene Danke: 21



BeitragVerfasst: Mi 17.08.11 13:23 
naja du hast eine liste von warnung und alert, gehst aber davon aus das es nur eine konfiguration gibt--> nur eine warnung und ein alert-->
du brauchst eine zweite klasse mit einer liste von konfigurationen, dann kannst du die de- und serialisieren
HoPPeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Mi 17.08.11 13:44 
ähh und wie muss das ganze dann aussehen :roll: ?

mfg
dark-destination1988
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 178
Erhaltene Danke: 21



BeitragVerfasst: Mi 17.08.11 14:20 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
public Class Sensor
{
   public List<Config>{get;set;}
//deine serialize funktionen
}


Moderiert von user profile iconTh69: C#-Tags hinzugefügt - bitte denke demnächst selber daran