我一直在网上寻找一些类似的问题,但没有找到解决我问题的答案。我正在尝试从类(注册信息)中获取数据并将其放入 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).
我对自己做错了什么感到困惑,并且非常感谢一些帮助。我还是个网络新手,所以文章链接对我帮助不大。正确的语法,或者解释我应该做什么以及为什么会很棒!
PS - 我在 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){
对于任何对齐错误,我深表歉意。
编辑:删除了不必要的代码。