質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

10443閲覧

XMLのシリアライズで例外が出る

sakumisan4201

総合スコア17

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2018/05/31 11:44

Unityでxmlにデータを保存するためにこんな感じにコードを書きました。

C#

1 /// <summary> 2/// 保存 3/// </summary> 4/// <param name="path">xmlファイルの場所</param> 5/// <param name="obj">シリアライズするオブジェクト</param> 6public static void Save(string path, System.Object obj) 7{ 8 Type type = obj.GetType(); 9 XmlSerializer ser = new XmlSerializer(type); 10 StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8); 11 12 ser.Serialize(sw, obj); 13 sw.Close(); 14 }

こうやってSave()を呼び出しました。

C#

1 string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\Roaming\qqqq"; 2 var task1 = new Task(() => SavedataManager.Save( path + "Player.xml", player)); 3 task1.Start(); 4 task1.Wait();

そして呼び出すと

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). System.Xml.Serialization.TypeScope.GetEnumeratorElementType (System.Type type, System.Xml.Serialization.TypeFlags& flags) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.TypeScope.ImportTypeDesc (System.Type type, System.Reflection.MemberInfo memberInfo, System.Boolean directReference) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.TypeScope.GetTypeDesc (System.Type type, System.Reflection.MemberInfo source, System.Boolean directReference, System.Boolean throwOnError) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.StructModel.GetPropertyModel (System.Reflection.PropertyInfo propertyInfo) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.StructModel.GetFieldModel (System.Reflection.MemberInfo memberInfo) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (System.Xml.Serialization.StructMapping mapping, System.Xml.Serialization.StructModel model, System.Boolean openModel, System.String typeName, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping (System.Xml.Serialization.StructModel model, System.String ns, System.Boolean openModel, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (System.Xml.Serialization.StructMapping mapping, System.Xml.Serialization.StructModel model, System.Boolean openModel, System.String typeName, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping (System.Xml.Serialization.StructModel model, System.String ns, System.Boolean openModel, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (System.Xml.Serialization.StructMapping mapping, System.Xml.Serialization.StructModel model, System.Boolean openModel, System.String typeName, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping (System.Xml.Serialization.StructModel model, System.String ns, System.Boolean openModel, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (System.Xml.Serialization.StructMapping mapping, System.Xml.Serialization.StructModel model, System.Boolean openModel, System.String typeName, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping (System.Xml.Serialization.StructModel model, System.String ns, System.Boolean openModel, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeModel model, System.String ns, System.Xml.Serialization.XmlReflectionImporter+ImportContext context, System.String dataType, System.Xml.Serialization.XmlAttributes a, System.Boolean repeats, System.Boolean openModel, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) Rethrow as InvalidOperationException: There was an error reflecting type 'Player'. System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeModel model, System.String ns, System.Xml.Serialization.XmlReflectionImporter+ImportContext context, System.String dataType, System.Xml.Serialization.XmlAttributes a, System.Boolean repeats, System.Boolean openModel, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeModel model, System.String ns, System.Xml.Serialization.XmlReflectionImporter+ImportContext context, System.String dataType, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportElement (System.Xml.Serialization.TypeModel model, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.String defaultNamespace) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlSerializer..ctor (System.Type type) (at <7fd195060d8c41448694ab221d3b56ca>:0) SavedataManager.Save (System.String path, System.Object obj) (at Assets/AST/SavedataManager/script/SavedataManager.cs:17) GameController.<Save>m__0 () (at Assets/script/GameController.cs:47) System.Threading.Tasks.Task.InnerInvoke () (at <e1a80661d61443feb3dbdaac88eeb776>:0) System.Threading.Tasks.Task.Execute () (at <e1a80661d61443feb3dbdaac88eeb776>:0) Rethrow as AggregateException: One or more errors occurred. System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) (at <e1a80661d61443feb3dbdaac88eeb776>:0) System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) (at <e1a80661d61443feb3dbdaac88eeb776>:0) System.Threading.Tasks.Task.Wait () (at <e1a80661d61443feb3dbdaac88eeb776>:0) GameController.Save (System.Boolean isExit) (at Assets/script/GameController.cs:49) UnityEngine.Events.InvokableCall`1[T1].Invoke (T1 args0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:207) UnityEngine.Events.CachedInvokableCall`1[T].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:345) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:70) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem.Update()

例外の原因、アドバイスなどをしていただけたら助かります。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

Unityは詳しくないので、外しているかも知れませんが、、、

var task1 = new Task(() => SavedataManager.Save( path + "Player.xml", player));

player というオブジェクトを シリアライズしているという事ですね。
このオブジェクトの内部に、Unityのオブジェクトを含んでいないでしょうか?
そこでエラーを出しているように見えます。
シリアライズ対象としたいデータのみで構成されたオブジェクト(クラス)を作成し、それを指定してシリアライズすべきと考えます。

投稿2018/05/31 11:53

pepperleaf

総合スコア6383

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

XmlSerializerは、シリアライズするオブジェクトのクラスが、引数のないコンストラクタがある、シリアライズ対象のクラス変数はpropertyでなければならない、などなどの制約があったと思いますんでそこらへん確認してみては。

オブジェクトの内容をXMLファイルに保存(シリアル化)する

この方法によりXMLファイルに書き込まれるメンバは、パブリックメンバであるフィールドとプロパティだけです。さらに、読み込み、あるいは書き込みしか許可されていないプロパティは、パブリックメンバであっても書き込まれません。また、シリアル化するオブジェクトのクラスには、規定のコンストラクタ(パラメータを持たないコンストラクタ)が必要です。

投稿2018/05/31 14:04

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問