Autor Beitrag
GURKE deluxe
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 91
Erhaltene Danke: 1

Win 7 Home Premium x64, Win XP Home Edition v2002
C# Microsoft Visual C# 2010 Express
BeitragVerfasst: So 06.01.13 14:21 
Hallo,
Ich habe folgenden Code zum Speichern eines objektes:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
                SpeicherKlasse Speicherdatei = new SpeicherKlasse(Karte, Ampelliste);
                XmlSerializer ser = new XmlSerializer(typeof(SpeicherKlasse), new Type[]{typeof(Feld), typeof(Ampeln)});
                TextWriter tw = new StreamWriter(sfd.FileName);
                ser.Serialize(tw, Speicherdatei);
                tw.Dispose();
                tw.Close();


Speicherklasse besteht aus Feldern und Ampeln. Diese wiederrum bestehen aus (Array)Double, Integer, Strings, Booleans und (List/Array) Points. Die Datei, die ich abspeichere sieht so aus:
<SpeicherKlasse/>

In Speicherdatei steht allerdings eine ganze Menge drin:
ausblenden volle Höhe 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:
-    Speicherdatei  {WindowsFormsApplication1.SpeicherKlasse}  WindowsFormsApplication1.SpeicherKlasse
-    Ampel  Count = 4  System.Collections.Generic.List<WindowsFormsApplication1.Ampeln>
-    [0]  {WindowsFormsApplication1.Ampeln}  WindowsFormsApplication1.Ampeln
-    Ampelposition  {X = 81 Y = 63}  System.Drawing.Point
    IsEmpty  false  bool
    X  81  int
    Y  63  int
+    Statische Member  System.Drawing.Point  System.Drawing.Point
+    Nicht öffentliche Member    
-    Haltelinie  Count = 2  System.Collections.Generic.List<System.Drawing.Point>
+    [0]  {X = 95 Y = 83}  System.Drawing.Point
+    [1]  {X = 95 Y = 83}  System.Drawing.Point
+    Rohdatenansicht    
    Herkunft  0  int
    Name  "0"  string
+    [1]  {WindowsFormsApplication1.Ampeln}  WindowsFormsApplication1.Ampeln
+    [2]  {WindowsFormsApplication1.Ampeln}  WindowsFormsApplication1.Ampeln
+    [3]  {WindowsFormsApplication1.Ampeln}  WindowsFormsApplication1.Ampeln
+    Rohdatenansicht  Count = 4  System.Collections.Generic.List<WindowsFormsApplication1.Ampeln>
-    Karte  {WindowsFormsApplication1.Feld[40000]}  WindowsFormsApplication1.Feld[]
-    [0]  {WindowsFormsApplication1.Feld}  WindowsFormsApplication1.Feld
    bAutoVorhanden  false  bool
    bLetztesFeld  false  bool
+    dAbstand  {double[4]}  double[]
    dAutoGeschwindigkeit  0.0  double
    dkmhlimit  2.0  double
    iAmpel  -1  int
    iAutoHerkunft  0  int
    iAutoID  0  int
    iAutoRichtung  0  int
    irestgelbzeit  0  int
+    pAutoPosition  {X = 0 Y = 0}  System.Drawing.Point
+    pLetztesFeld  {System.Drawing.Point[4]}  System.Drawing.Point[]
+    pNaechstesFeld  {System.Drawing.Point[4]}  System.Drawing.Point[]
+    Position  {X = 0 Y = 0}  System.Drawing.Point


Das ist nur ein kleiner Ausschnitt, tatsächlich ist das gesehene nur eine Ampel und ein Feld.
Wieso speichert er nun nichts ab und wie bekomme ich ihn dazu es zu tun?

Gruß
Julian
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 06.01.13 15:05 
Kann das damit zusammenhängen, dass Du erst Dispose aufrufst und dann erst Close? Aber eigentlich sollte Dispose ja auch Close aufrufen ... :gruebel:

Versuch's mal so, das ist eh sauberer weil Exception-sicher:

ausblenden C#-Quelltext
1:
2:
3:
4:
SpeicherKlasse Speicherdatei = new SpeicherKlasse(Karte, Ampelliste);
XmlSerializer ser = new XmlSerializer(typeof(SpeicherKlasse), new Type[]{typeof(Feld), typeof(Ampeln)});
using(var tw = new StreamWriter(sfd.FileName))
  ser.Serialize(tw, Speicherdatei);


Bin mir aber nicht sicher, ob das hilft :nixweiss:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: So 06.01.13 15:11 
Zeig' mal ein bisschen Quellcode der SpeicherKlasse.
Sind die ganzen Sachen auch public Properties?
GURKE deluxe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 91
Erhaltene Danke: 1

Win 7 Home Premium x64, Win XP Home Edition v2002
C# Microsoft Visual C# 2010 Express
BeitragVerfasst: So 06.01.13 15:31 
Speicherklasse:
ausblenden 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:
public class SpeicherKlasse
    {
        protected Feld[] Karte;
        protected List<Ampeln> Ampel;

        public SpeicherKlasse() { }
        public SpeicherKlasse(Feld[,] Karte, List<Ampeln> Ampel)
        {
            this.Karte = new Feld[Karte.Length];
            for (int i = 0; i < Karte.GetLength(0); i++)
                for (int j = 0; j < Karte.GetLength(1); j++)
                    if (Karte[i, j] != null)
                        this.Karte[i * Karte.GetLength(1) + j] = Karte[i, j];
            this.Ampel = Ampel;
        }

        public Feld[] GibKarte()
        {
            return Karte;
        }

        public List<Ampeln> GibAmpeln()
        {
            return Ampel;
        }
    }


Feld:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
public class Feld
    {
        protected Point Position = new Point();
        protected int iAmpel = -1;
        protected double dkmhlimit;
        protected int irestgelbzeit;
        protected int iAutoID;
        protected int iAutoHerkunft;
        protected int iAutoRichtung;
        protected double dAutoGeschwindigkeit;
        protected Point pAutoPosition;
        protected double[] dAbstand = new double[4] { 0000 };
        protected bool bAutoVorhanden;
        protected Point[] pNaechstesFeld = new Point[4];
        protected Point[] pLetztesFeld = new Point[4];
        protected bool bLetztesFeld;

ganze viele geber und setzer


Ampel:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
public class Ampeln
    {
        public Point Ampelposition;
        public List<Point> Haltelinie;
        public int Herkunft;
        public string Name;

        public Ampeln() { }
        public Ampeln(string Name)
        {
            this.Name = Name;
        }
        public Ampeln(string Name, Point Ampelposition, List<Point> Haltelinie, int Herkunft)
        {
            this.Ampelposition = Ampelposition;
            this.Haltelinie = Haltelinie;
            this.Herkunft = Herkunft;
            this.Name = Name;
        }

ganze viele geber und setzer


Mit Dispose/Close vertauscht gehts auch nicht ...
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: So 06.01.13 15:41 
Wie ich vermutet hatte :mrgreen:

Es werden nur Properties serialisiert. Du hast überall Felder verwendet.
Konkret sieht das z.B. so aus:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
public class SpeicherKlasse
    {
// Automatische Property
        public Feld[] Karte { get; set; }
// Oder manuell und readonly
        private List<Ampel> _ampel;
        public List<Ampeln> Ampel
        {  
            get  
            {  
                return _ampel;  
            }  
        }

Die Initialisierungen musst du bei den automatischen Properties leider in den Konstruktor verschieben.

Das sind dann auch richtige getter und setter, deine gibxyz() Methoden darfst du dann ruhig löschen ;-)
Siehe auch: openbook.galileocomp...csharp/kap18.htm#t21

Für diesen Beitrag haben gedankt: GURKE deluxe