Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chietor · Mar 04, 2018 at 10:01 PM · databasexmlinputfieldxmlserializer

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> ();
 }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chietor · Mar 05, 2018 at 05:59 AM 0
Share

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.

avatar image Chietor Chietor · Mar 05, 2018 at 06:32 AM 0
Share

I already try it and this error come:

Assets/ItemDisplay.cs(14,17): error CS0030: Cannot convert type string' to ItemEntry'

avatar image fafase Chietor · Mar 05, 2018 at 06:50 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges