By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

xml serialization question

100+
P: 112
hi,
i am trying to write all the data for my games level to an xml file using c# xml serializer.

i got it to work but for any of my classes below the Level class none of the fields are written.

for instance, a Level has a List of Ball objects and a list of brick objects (guess theres not too many hints about what kind of game it is lol)
each file has fields that i marked with
[xmlattribute] or [xmlelement] or [xmlarray] ect. when i output the file, there are <Ball \> tags for each ball and <brick \> tags for each brick.

what i would like to see is something like:
<Level name="level1>
<Ball x="0" y="1" \>
<\level>
something alpong those lines, where the element and attribute tabs of the Ball class are writted to the file as well.

The attributes, elements and arrays and things from the Level class are all written corretly.

is there some special way that i need to setup the serializer for it to look into these seperate calsses for the serialization??

thanks alot,
ken
Dec 19 '08 #1
Share this Question
Share on Google+
6 Replies


P: 61
@drsmooth
Check the following boldface code.Hope this helps you.
Expand|Select|Wrap|Line Numbers
  1. [XmlRoot]
  2. public class Level
  3. {
  4.     [XmlAttribute]
  5.     public int lvl;
  6.    [XmlElement]
  7.     public Ball[] ballColl;
  8. }
  9. public class Ball
  10. {           
  11.     [XmlAttribute]
  12.     public int x, y;
  13. }
Dec 20 '08 #2

100+
P: 112
originally i had them in seperate files, i doint know if that makes a difference?

after reading your post, i tried moving the Ball( actually its actor in the specifics of my program) into the same file and still no luck, theres enough <Actor \> tags to account for the number of things i created in the array but theyre all empty.

Heres the code where i convert the Actor[list] into an array for the xml processing:
Expand|Select|Wrap|Line Numbers
  1. [XmlElement]
  2.         public Actor[] Actors
  3.         {
  4.             get
  5.             { 
  6.                 Actor[] items = new Actor[balls.Count];
  7.                 balls.CopyTo(items);
  8.                 return items;
  9.             }
  10.             set
  11.             {
  12.                 if (value == null) return;
  13.                 Actor[] items = (Actor[])value;
  14.                 balls.Clear();
  15.                 foreach (Actor item in items)
  16.                     balls.Add(item);
  17.             }
  18.         }
and heres the actor classs:
Expand|Select|Wrap|Line Numbers
  1. public class Actor
  2.     {
  3.         [XmlAttribute]
  4.         private int myX;
  5.         [XmlAttribute]
  6.         private int myY;
  7.         [XmlAttribute]
  8.         private int mySize;
  9.         [XmlAttribute]
  10.         private int vY;
  11.         [XmlAttribute]
  12.         private int vX;
  13.         public Actor() { }
  14.         public Actor(int x, int y, int s)
  15.         {
  16.             myX = x;
  17.             myY = y;
  18.             mySize = s;
  19.             vY = 0;
  20.             vX = 0;
  21.         }
  22.         public void move()
  23.         {
  24.             myX += vX;
  25.             myY += vY;
  26.         }
  27.         public void reverseY() { vY *= -1; }
  28.         public void reverseX() { vX *= -1; }
  29.         public void increaseVX(int x) { vX += x; }
  30.         public void increaseVY(int x) { vY += x; }
  31.         public void stopX() { vX = 0; }
  32.         public void stopY() { vY = 0; }
  33.         public int getX() { return myX; }
  34.         public int getY() { return myY; }
  35.         public int getVX() { return vX; }
  36.         public int getVY() { return vY; }
  37.         public int getSize() { return mySize; }
  38.     }
Dec 20 '08 #3

P: 61
Try to change the access specifiers in your Actor class from private to public.
Dec 21 '08 #4

100+
P: 112
that did the trick!

now that you mentioned it it was like "ohhhhhhh" and i realized that it makes sense now why that was happening.

thanks alot,
ken
Dec 21 '08 #5

P: 2
I have an object

Class myclass
{
public int first;
public int second;
}

when i serialize the object i get an xml like

<myclass>
<first>123</first>
<second>456</second>
</myclass>

i want my xml to be

<myclass>
<properties>
<first>123</first>
<second>456</second>
</properties>
</myclass>


Please help...
Apr 1 '09 #6

P: 61
Expand|Select|Wrap|Line Numbers
  1. public class myclass
  2.         {
  3.             [XmlElement]
  4.             public Property Properties;
  5.             public myclass()
  6.             {
  7.                 Properties = new Property();
  8.             }
  9.         }
  10.  
  11.         public class Property
  12.         {
  13.             [XmlElement]
  14.             public int first;
  15.             [XmlElement]
  16.             public int second;
  17.         }
  18.  
Hope this generates the required XML by you.
Apr 2 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.