Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中的XMLSerializer不包括类的所有属性_C#_Xml - Fatal编程技术网

C#中的XMLSerializer不包括类的所有属性

C#中的XMLSerializer不包括类的所有属性,c#,xml,C#,Xml,您好,我有两个类,我想使用C#中的XMLSerializer对它们进行序列化: 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"

您好,我有两个类,我想使用C#中的XMLSerializer对它们进行序列化:

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 a un data stream para enviar y recibir datos
    //Tambien lo debemos modificar para que se acople a 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;


    }

}
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;
    }


}
这就是我用来测试代码的方法:

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 ();
我得到的xml是:

<?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>

无效的
6.
如您所见,Bullet数组不包含我在类中定义的任何属性


有人能帮我吗?

XmlSerializer没有考虑私有字段,这就是为什么它们在输出中没有显示出来。如果将这些变量公开并用XmlAttribute标记,则可以在结果XML中看到它们:

如果你像这样改变它

[XmlAttribute("id")]
public int id;
你会得到:

<?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>

无效的
6.

您需要将字段公开(或者更好地使用公共属性)
XmlSerializer
仅序列化公共字段和属性。非常感谢,这解决了我的问题:)