- Home /
How can I save a xml file but the data will be coming from a Input Field?
I try using this code I found in Youtube but I want to get the text/data from a input field, Can anyone please help me? public string information is the real code but I try to modify it and change it to public InputField information and it gives me this error -
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).
public class XMLManager : MonoBehaviour
{
public static XMLManager ins;
void Awake(){
ins = this;
}
public ItemDatabase itemDB;
public void SaveItems(){
XmlSerializer serializer = new XmlSerializer (typeof(ItemDatabase));
FileStream stream = new FileStream (Application.dataPath + "/XML File/sample.xml", FileMode.Create);
serializer.Serialize (stream, itemDB);
stream.Close ();
}
}
[System.Serializable]
public class ItemEntry{
public InputField information
}
[System.Serializable]
public class ItemDatabase{
public List<ItemEntry> list = new List<ItemEntry> ();
}
Answer by fafase · Mar 04, 2018 at 10:26 PM
The issue most likely comes from the list of ItemEntry in ItemDatabase.
This is a list of wrapped InputField which is a MonoBehaviour component and contains a reference to Transform. This is possibly the reason of the error you are showing. Fact is, I'd say more errors are coming but the compiler stopped there.
The solution would be to discard the serialization of InputField but only of string-
[System.Serializable]
public class ItemDatabase{
public List<string> list = new List<string> ();
public ItemDatabase(){}
public void SetValues(IEnumerable<InputField> ifs){
this.list = new List<string>();
if(ifs == null){return;}
foreach(InputField input in ifs){
if(input == null) { continue;}
if(string.isNullOrEmpty(inputField.text) == true){ return; }
this.list.Add(inputField.text);
}
}
}
The IEnumerable of InputField may come from wherever you store the InputFields.
Any reason why XML over Json? The latter is lighter and I'd say easier to use (personal opinion).
This would be Json:
public class Controller:MonoBehaviour
{
[SerializeField] private InputField[] inputs = null;
public void SaveData(){
ItemDatabase items = new ItemDatabase(this.inputs as IEnumerable<InputField>);
string json = JsonUtility.ToJson(items);
System.IO.File.WriteAllText(path, json);
}
}
Done.
This would be my first time using X$$anonymous$$L. I use X$$anonymous$$L like this:
Also called a Counting Frame, is a calculating tool that was used in Europe, China and Russia, centuries before the adoption of the written Hindu-Arabic numeral system.
but thanks for the answer I will try it.
I already try it and this error come:
Assets/ItemDisplay.cs(14,17): error CS0030: Cannot convert type string' to
ItemEntry'
Somewhere you are still using this ItemEntry type. I would totally get rid of it as it does not bring any value. ItemDatabase contains a list of string, nothing else. Then you have a collection of inputFields and you pass them to the ctor of ItemDatabase. In the ctor, the string from the text is added to the list. This is it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612153939im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Xml Saving and Loading Problems 1 Answer
How to type Color32 format into XML? 1 Answer
Help on making a Database. 0 Answers
loading XML quiz 1 Answer
Columns in XML serialization 0 Answers