Autor Beitrag
AeroX
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Do 20.03.08 19:18 
hallo,
ich habe mir ein kleines Notiz-programm gemacht,
jedoch speichert es die Daten nicht wieder ab!

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:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
        FileStream FS;
        TextWriter TW;
        StreamReader SR;
        XmlSerializer XS = new XmlSerializer(typeof(DataClass));
        DataClass DC = new DataClass();

...

        /*
         *    L A D E N ...
´        */

        public void LoadXml()
        {
            if (File.Exists(FILE))
            {
                try
                {
                    SR = new StreamReader(FILE);
                    DataClass DC = (DataClass)XS.Deserialize(SR);
                    SR.Close();
                    iNotesLength = DC.Name.Count;
                }
                catch (Exception e)
                {
                    MessageBox.Show("Fehler beim Laden!\nError: " + e.Message);
                }
            }
        }



        /*
         *    S P E I C H E R N
         */

        public void SaveXml()
        {
            try
            {
                FS = File.Open(FILE, FileMode.Create);
                TW = new StreamWriter(FS);
                XS.Serialize(TW, DC);
                TW.Close();
                FS.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("Fehler beim speichern!\nError: "+e.Message);
            }
        }
...

    public class DataClass
    {
        public List<string> Name = new List<string>();
        public List<string> Text = new List<string>();
    }


Laden funktioniert, das habe ich schon getestet.

aber er speichert einfach nur, leere variablen ab.

Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
AeroX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: So 23.03.08 22:45 
kann mir keiner helfen?
Robert_G
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416


Delphi32 (D2005 PE); Chrome/C# (VS2003 E/A, VS2005)
BeitragVerfasst: So 23.03.08 23:10 
user profile iconAeroX hat folgendes geschrieben:
kann mir keiner helfen?
Du lädst es nicht.
Du lädst es in eine lokale Variable, anstatt in das Feld. Beim Speichern, willst du hingegen das Feld speichern.

Entferne einfach mal die lokale Variable beim Laden.
AeroX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Mo 24.03.08 01:24 
welche variable meinst du?
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mo 24.03.08 12:19 
user profile iconAeroX hat folgendes geschrieben:
welche variable meinst du?

Vermutlich das hier im LoadXml:
ausblenden C#-Quelltext
1:
2:
3:
4:
//  falsch:
DataClass DC = (DataClass)XS.Deserialize(SR);
//  richtig:
DC = (DataClass)XS.Deserialize(SR);

Durch die lokale Deklaration entsteht ein lokales Objekt, das am Ende des try-Blocks schon wieder verschwindet.

Jürgen
AeroX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Mo 24.03.08 21:35 
ok, danke dir!