XML typeof XmlSerializer error

Started by
2 comments, last by akira32 14 years, 6 months ago
XmlSerializer serializer = new XmlSerializer(typeof(SaveStageData));//<==problem is here(line113) When I save data, the program will break at the line and appears the error message as below: http://cid-fbeb6373d9321a7f.skydrive.live.com/self.aspx/Questions/XNAqq/xmlError2.JPG Could somebody tell me how to solve this problem? The structure of SaveStageData is wrong?

1 namespace UtilityProject.Utility   
2 {   
3     public enum ENUM_OBJECT_TYPE   
4     {   
5         Role,   
6         Object,   
7         Floor,   
8     }   
9   
10     [Serializable]   
11     public class ObjectBase   
12     {   
13         public ObjectBase(ENUM_OBJECT_TYPE type,int _nModelID,Point _point2D, Vector3 _translate, Vector3 _rotate, Vector3 _scale)   
14         {   
15             nType = type;   
16   
17             point2D = _point2D;   
18             translate = _translate;   
19             rotate = _rotate;   
20             scale = _scale;   
21   
22             nModelID = _nModelID;   
23         }   
24   
25         public ObjectBase(ENUM_OBJECT_TYPE type)   
26         {   
27             nType = type;   
28   
29             point2D = new Point(-1, -1);   
30             translate = new Vector3(-1, -1, -1);   
31             rotate = new Vector3(0, 0, 0);   
32             scale = new Vector3(1, 1, 1);   
33   
34             nModelID = 0;   
35         }   
36   
37         public void SetRotation(Vector3 _rotate)   
38         {   
39             rotate = _rotate;   
40         }   
41   
42         public Matrix GetWorld()   
43         {   
44             return matWorld;   
45         }   
46   
47         public void Update()   
48         {   
49             matWorld = Matrix.CreateScale(scale) * Matrix.CreateFromYawPitchRoll(rotate.Y, rotate.X, rotate.Z) * Matrix.CreateTranslation(translate);   
50         }   
51   
52         public void Draw(Camera camera)   
53         {   
54   
55         }   
56   
57         public Point point2D;   
58         public Vector3 translate;   
59         public Vector3 rotate;// Angles are measured clockwise    
60         public Vector3 scale;   
61   
62         public int nModelID;   
63   
64         public ENUM_OBJECT_TYPE nType;   
65   
66         private Matrix matWorld;   
67     }   
68   
69     public class RoleDraw : ObjectBase   
70     {   
71         public RoleDraw(int _nModelID, Point _point2D, Vector3 _translate, Vector3 _rotate, Vector3 _scale)   
72             : base(ENUM_OBJECT_TYPE.Role,_nModelID,_point2D,_translate,_rotate,_scale)   
73         {   
74   
75         }   
76     }   
77   
78     [Serializable]   
79     public struct SaveStageData   
80     {   
81         public List<ObjectBase> objectlist;   
82     }   
83   
84   
85     public class ObjectManager : Microsoft.Xna.Framework.DrawableGameComponent   
86     {   
87         public SaveStageData saveStageData;   
88   
89         public ObjectManager(Microsoft.Xna.Framework.Game game,Camera camera,ModelManager _modelManager)   
90             : base(game)   
91         {   
92             game1 = game;   
93             m_Camera = camera;   
94   
95             modelManager = _modelManager;   
96   
97             saveStageData = new SaveStageData();   
98             saveStageData.objectlist = new List<ObjectBase>();   
99         }   
100            
101         public bool SaveStageData()   
102         {   
103             IAsyncResult result;   
104   
105             result = Guide.BeginShowStorageDeviceSelector(PlayerIndex.One, null, null);   
106             StorageDevice device = Guide.EndShowStorageDeviceSelector(result);   
107   
108             //   
109             StorageContainer container = device.OpenContainer("XNA MapSave");   
110             string filename = Path.Combine(container.Path, "stage.xml");   
111   
112             FileStream stream = File.Open(filename, FileMode.Create);   
113             XmlSerializer serializer = new XmlSerializer(typeof(SaveStageData));//<==problem is here   
114             serializer.Serialize(stream, saveStageData);   
115   
116             stream.Close();   
117             container.Dispose();   
118   
119             return true;   
120         }   
121   
122   
123         public void RemoveObject(ObjectBase delObect)   
124         {   
125             saveStageData.objectlist.Remove(delObect);   
126         }   
127   
128         public void AddModel(int _nModelID, Point _point2D, Vector3 _translate, Vector3 _rotate, Vector3 _scale)   
129         {   
130             saveStageData.objectlist.Add(new ObjectBase(ENUM_OBJECT_TYPE.Role,_nModelID, _point2D, _translate, _rotate, _scale));   
131         }   
132   


[Edited by - akira32 on October 14, 2009 11:49:15 AM]
akira32 編程之家 Yahoohttp://tw.myblog.yahoo.com/akira32-akira32
Advertisement
If you'd looked at the exception details you'd have seen the problem:

Quote:{"WindowsGame1.ObjectBase cannot be serialized because it does not have a parameterless constructor."}

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Quote:Original post by Machaira
If you'd looked at the exception details you'd have seen the problem:

Quote:{"WindowsGame1.ObjectBase cannot be serialized because it does not have a parameterless constructor."}


Thanks!! I had solved this problem.
I do not see the exception details. Does it output to the output window?
I use XNA 3.0.
But I have another problem, If I modify the line 130 to be
saveStageData.objeclist.Add(new RoleDraw(,_nModelID, _point2D, _translate, _rotate, _scale));

128 public void AddModel(int _nModelID, Point _point2D, Vector3 _translate, Vector3 _rotate, Vector3 _scale)
129 {
130 saveStageData.objectlist.Add(new ObjectBase(ENUM_OBJECT_TYPE.Role,_nModelID, _point2D, _translate, _rotate, _scale));
131 }

The new error message will appear again.(at the line of serializer.Serialize(stream, saveStageData);).
The new error message is http://cid-fbeb6373d9321a7f.skydrive.live.com/self.aspx/Questions/XNAqq/XmlError3.JPG
Could somebody tell how to solve this problem?
I had add a default constructor to RoleDraw and even add [Serializable] to RoleDraw. It does not work.
akira32 編程之家 Yahoohttp://tw.myblog.yahoo.com/akira32-akira32
I had solved the new problem by seeing the inner exception message in Local Window.
The answer is using [XmlInclude(typeof(RoleDraw)) to replace [Serializable]

//[Serializable]
[XmlInclude(typeof(EnemyDraw))]
public class SaveStageData
{
public SaveStageData()
{
enemylist = new List<ObjectBase>();
}

public List<ObjectBase> enemylist;
}
akira32 編程之家 Yahoohttp://tw.myblog.yahoo.com/akira32-akira32

This topic is closed to new replies.

Advertisement