473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

xml serialization question

112 100+
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
6 1216
Ramk
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
drsmooth
112 100+
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
Ramk
61
Try to change the access specifiers in your Actor class from private to public.
Dec 21 '08 #4
drsmooth
112 100+
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
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
Ramk
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

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

Similar topics

16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
0
by: eSapient | last post by:
I generated serialization/deserialization code for this schema using the xsd tool: <?xml version="1.0" encoding="UTF-8"?> <xs:schema elementFormDefault="qualified"...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
0
by: groovyghoul | last post by:
Hi I have the following XML file: =========================================================== <?xml version="1.0" encoding="UTF-16"?> <Policy xmlns="http://tempuri.org/richard.xsd"> <TransType...
0
by: nobin01 | last post by:
Dear sir; I want ur Help in serialization.I know serialization.I Know binary,soap and xmlserialization also.But i want ur help in following topics.pls help me as soon as possible.I have search in...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.