XmlSerializer抛出InvalidOperationException c#

时间:2013-10-15 23:33:04

标签: c# xml networking unity3d xmlserializer

我一直在线查看并通过一些类似的问题找不到解决问题的答案。我试图从类中获取数据(注册信息)并将其放入xml文件中。该计划不会超越这条线:

var serializer = new XmlSerializer(typeof(AccountStructure));

错误:

   InvalidOperationException: To be XML serializable, types which inherit from 
 IEnumerable must have an implementation of Add(System.Object) at all levels of 
 their inheritance hierarchy. UnityEngine.Transform does not implement 
 Add(System.Object).

我对我做错了什么感到难过,并且非常感谢一些帮助。我仍然是网络新手,所以文章的链接并没有帮助我。正确的语法,或解释我应该做什么以及为什么会是惊人的!

P.S。 - 我在Unity游戏引擎中使用MonoDevelop C#

以下是我的课程代码:

帐户信息类:错误在哪里

 using UnityEngine;
 using System.Collections.Generic;
 using System.Xml;
 using System.Xml.Serialization;
 //using System.Runtime.Serialization;
  using System.IO;
 [XmlRoot("UserInfo")]
 public class AccountStructure : MonoBehaviour {


    [XmlElement]
    public string Username;
    [XmlElement]
    public string Password;
    [XmlElement]
    public string Email;
    [XmlElement]
    public int Age;
    [XmlElement]
    public bool isMember;

    public void Save(string path){
        Debug.Log("1");
        //DataContractSerializer dcs = new DataContractSerializer(typeof(AccountStructure));
        var serializer = new XmlSerializer(typeof(AccountStructure));
        Debug.Log("2");
        var stream = new FileStream(path, FileMode.Create);
        serializer.Serialize(stream, this);
        stream.Close();
    }
    public void Add(System.Object obj){

    }
 }

服务器RPC

[RPC]

public void Register(string Username, string Password, string Email, int Age, NetworkPlayer Player){
    if(Network.isServer){
        RegisterData.Username = Username;
        RegisterData.Password = Password;
        RegisterData.Email = Email;
        RegisterData.Age = Age;
        RegisterData.isMember = true;
        log += " Sending Data";
        RegisterData.Save(Path.Combine(Application.persistentDataPath, "info.xml"));
        log += " Sent Data";
    }
}

客户来电

[RPC]

public void Register(string Username, string Password, string Email, int Age, NetworkPlayer Player){

我为任何对齐错误道歉。

编辑:删除了不必要的代码。

0 个答案:

没有答案