简体   繁体   English

反\\序列化对象列表XML

[英]De\Serializing list of objects XML

I have been tried to do deserialize and it doesn't work. 我已经尝试过进行反序列化,但是它不起作用。 Why it happen ? 为什么会发生? Im tring to deserialize list of object as I serialize list of object. 尝试序列化对象列表时反序列化对象列表。 Is it possible ? 可能吗 ? Im trying something but it doens't the correct way. 我正在尝试一些东西,但它不是正确的方法。 Thanks on help. 谢谢你的帮助。

My code : 我的代码:

in the main : 在主要方面:

Serialize(lstObservers);

 public void Serialize(List<Observer> list)
        {
           XmlSerializer serializer = new XmlSerializer(typeof(List<Observer>));
            using ( TextWriter writer = new StreamWriter( @"C:\Users\user\Desktop\MapSample\bin\Debug\ListObserver.xml"))
            {
                serializer.Serialize(writer, list);
            }  
        }

   private void DiserializeFunc()
        {
            var myDeserializer = new XmlSerializer(typeof(List<Observer>));
        using (var myFileStream = new FileStream(@"C:\Users\user\Desktop\MapSample\bin\Debug\ListObserver.xml", FileMode.Open))
        {
            listObservers = (List<Observer>)myDeserializer.Deserialize(myFileStream);
        }

        }

the class observer : 班级观察员:

    [Serializable()]
    public class Observer : MapObject
    {
        private int ID_Observer { get; set; }
        private double azimuth;
        private double Long;
        private double Lat;
        private double Lenght;
        private bool haveConnection;
        private bool DrawAzimuth;
        private XmlSerializer ser;

        /// <summary>
        /// C'tor
        /// </summary>
        public Observer(int ID_Observer = 0, double azimuth = 0, double Lat = 0, double Long = 0, double Lenght = 0, bool haveConnection = true, bool DrawAzimuth = true)
        {
            this.ID_Observer = ID_Observer;
            this.azimuth = azimuth;
            this.Long = Long;
            this.Lat = Lat;
            this.haveConnection = haveConnection;
            this.DrawAzimuth = DrawAzimuth;
            this.Lenght = Lenght;
        }

        public Observer()
        {
            ser = new XmlSerializer(this.GetType());
        }
}

EDIT : my propose is to save list of objects.. if anyone have another idea i will be more than happy to learn. 编辑:我的建议是保存对象列表。如果有人有其他想法,我将非常乐于学习。 Thanks 谢谢

Private fields don't serialize. 专用字段不会序列化。 Make properties that are public: 公开属性:

      [Serializable()]
  public class Observer : MapObject
  {

    private XmlSerializer ser;

    public int ID_Observer { get; set; }
    public double azimuth { get; set; }
    public double Long { get; set; }
    public double Lat { get; set; }
    public double Lenght { get; set; }
    public bool haveConnection { get; set; }
    public bool DrawAzimuth { get; set; }

    /// <summary>
    /// C'tor
    /// </summary>
    public Observer(int ID_Observer = 0, double azimuth = 0, double Lat = 0, double Long = 0, double Lenght = 0, bool haveConnection = true, bool DrawAzimuth = true)
    {
      this.ID_Observer = ID_Observer;
      this.azimuth = azimuth;
      this.Long = Long;
      this.Lat = Lat;
      this.haveConnection = haveConnection;
      this.DrawAzimuth = DrawAzimuth;
      this.Lenght = Lenght;
    }

    public Observer()
    {
      ser = new XmlSerializer(this.GetType());
    }
  }

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM