xml - XMLSerializer in C# not including all attributes of a Class -


hello have 2 classes want serialize using xmlserializer in c#:

paquete.cs

using unityengine;  using system.collections; using system; using system.collections.generic; using system.text; using system.xml; using system.xml.serialization; using system.io;  [xmlroot("paquete")] public class paquete {      //el identificador del paquete. aqui ira la accion que se esta enviando en el juego      public enum identificador     {         moverizquierda,         moverderecha,         moverarriba,         moverabajo,         avanzar,         disparar,         null     }     public identificador identificadorpaquete;     public int jugador;       //[xmlarray("b"),xmlarrayitem("bullet")]     public list<bullet> bullets = new list<bullet> ();           // default constructor     public paquete()     {         this.identificadorpaquete = identificador.null;         this.jugador = -1;         this.bullets = new list<bullet> ();     }       // convierte un paquete un data stream para enviar y recibir datos     //tambien lo debemos modificar para que se acople las acciones del juego     public string getdatastream()     {         string result = "";         xmlserializer serializer = new xmlserializer (typeof(paquete));         stringwriter writer = new stringwriter ();         serializer.serialize (writer, this);         result = writer.tostring ();         debug.log ("el xml: "+result);         return result;       }  } 

bullet.cs

using system.collections; using system.xml; using system.xml.serialization; using system.io;  public class bullet  {      //[xmlattribute("id")]     int id;     //[xmlattribute("px")]     float px;     //[xmlattribute("py")]     float py;     //[xmlattribute("pz")]     float pz;     //[xmlattribute("rx")]     float rx;     //[xmlattribute("ry")]     float ry;     //[xmlattribute("rz")]     float rz;      public bullet(){         this.px = 0;         this.py = 0;         this.pz = 0;         this.rx = 0;         this.ry = 0;         this.rz = 0;         }     public bullet(int id,float px,float py, float pz,float rx,float ry,float rz){         this.id = id;         this.px = px;         this.py = py;         this.pz = pz;         this.rx = rx;         this.ry = ry;         this.rz = rz;     }   } 

this i´m using test code:

paquete p = new paquete (); p.jugador = 6; string s; p.bullets.add (new bullet(0,1,1,1,1,1,1)); p.bullets.add (new bullet (1,2, 2, 2, 2, 2, 2));  s = p.getdatastream (); 

and xml this:

<?xml version="1.0" encoding="utf-16"?> <paquete xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">   <identificadorpaquete>null</identificadorpaquete>   <jugador>6</jugador>   <bullets>     <bullet />     <bullet />   </bullets> </paquete> 

as can see, bullet array not contain of attributes defined in class.

can me out?

xmlserializer doesn't take private fields account that's why don't appaer in output. if make variables public , mark them xmlattribute can see them in result xml:

if change

[xmlattribute("id")] public int id; 

you'd get:

<?xml version="1.0" encoding="utf-16"?> <paquete xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">   <identificadorpaquete>null</identificadorpaquete>   <jugador>6</jugador>   <bullets>     <bullet id="0" />     <bullet id="1" />   </bullets> </paquete> 

Comments