xml serialization question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drsmooth
    New Member
    • Oct 2007
    • 112

    xml serialization question

    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
  • Ramk
    New Member
    • Nov 2008
    • 61

    #2
    Originally posted by drsmooth
    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
    Check the following boldface code.Hope this helps you.
    Code:
    [XmlRoot]
    public class Level
    {
        [XmlAttribute]
        public int lvl;
       [B][XmlElement][/B]
        public Ball[] ballColl;
    }
    public class Ball
    {           
        [B][XmlAttribute][/B]
        public int x, y;
    }

    Comment

    • drsmooth
      New Member
      • Oct 2007
      • 112

      #3
      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:
      Code:
      [XmlElement]
              public Actor[] Actors
              {
                  get
                  { 
                      Actor[] items = new Actor[balls.Count];
                      balls.CopyTo(items);
                      return items;
                  }
                  set
                  {
                      if (value == null) return;
                      Actor[] items = (Actor[])value;
                      balls.Clear();
                      foreach (Actor item in items)
                          balls.Add(item);
                  }
              }
      and heres the actor classs:
      Code:
      public class Actor
          {
              [XmlAttribute]
              private int myX;
              [XmlAttribute]
              private int myY;
              [XmlAttribute]
              private int mySize;
              [XmlAttribute]
              private int vY;
              [XmlAttribute]
              private int vX;
              public Actor() { }
              public Actor(int x, int y, int s)
              {
                  myX = x;
                  myY = y;
                  mySize = s;
                  vY = 0;
                  vX = 0;
              }
              public void move()
              {
                  myX += vX;
                  myY += vY;
              }
              public void reverseY() { vY *= -1; }
              public void reverseX() { vX *= -1; }
              public void increaseVX(int x) { vX += x; }
              public void increaseVY(int x) { vY += x; }
              public void stopX() { vX = 0; }
              public void stopY() { vY = 0; }
              public int getX() { return myX; }
              public int getY() { return myY; }
              public int getVX() { return vX; }
              public int getVY() { return vY; }
              public int getSize() { return mySize; }
          }

      Comment

      • Ramk
        New Member
        • Nov 2008
        • 61

        #4
        Try to change the access specifiers in your Actor class from private to public.

        Comment

        • drsmooth
          New Member
          • Oct 2007
          • 112

          #5
          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

          Comment

          • sprasanna100
            New Member
            • Apr 2009
            • 2

            #6
            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...

            Comment

            • Ramk
              New Member
              • Nov 2008
              • 61

              #7
              Code:
              public class myclass
                      {
                          [XmlElement]
                          public Property Properties;
                          public myclass()
                          {
                              Properties = new Property();
                          }
                      }
              
                      public class Property
                      {
                          [XmlElement]
                          public int first;
                          [XmlElement]
                          public int second;
                      }
              Hope this generates the required XML by you.

              Comment

              Working...