• 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 Ochreous · Dec 17, 2012 at 04:46 AM · c#xml

C# Saving Values to XML

Hi Everyone, I was wondering how would you save Values from a script to XML? I have the values setup in my script but I don't know how to save them to XML. Any help would be appreciated.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class ExampleScript : MonoBehaviour{
 public int ExampleInt;
 public bool ExampleBool;
 public float ExampleFloat;
 public string ExampleString;
 public int[] ExampleArray;
 public List<string> ExampleList  = new List<string>();
 //What comes next?
 }

Edit: I decided to go with the XML serialization tutorial at switchonthecode and tried to adapt it to my own code.However I have encountered this error no overload for SerializeToXML takes 0 arguments.I'm not sure what argument I want to put in that call. Also do you see any other problems within my code?

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     using System.Xml;
     using System.Xml.Serialization;
     using System.IO;
     public class ExampleScript : MonoBehaviour{
     public int ExampleInt;
     public bool ExampleBool;
     public float ExampleFloat;
     public string ExampleString;
     public int[] ExampleArray;
     public List<string> ExampleList  = new List<string>();
     }
     public class ExampleScriptSave: ExampleScript{
         [XmlElement("exampleArray")]
         public int[] exampleArray {
         get {
             return this.ExampleArray;
         }
         set {
             ExampleArray = value;
         }
     }
     [XmlElement("exampleBool")]
     public bool exampleBool {
         get {
             return this.ExampleBool;
         }
         set {
             ExampleBool = value;
         }
     }
     [XmlElement("exampleFloat")]
     public float exampleFloat {
         get {
             return this.ExampleFloat;
         }
         set {
             ExampleFloat = value;
         }
     }
     [XmlElement("exampleInt")]
     public int exampleInt {
         get {
             return this.ExampleInt;
         }
         set {
             ExampleInt = value;
         }
     }
     [XmlElement("exampleList")]
     public List<string> exampleList {
         get {
             return this.ExampleList;
         }
         set {
             ExampleList = value;
         }
     }
     [XmlElement("exampleString")]
     public string exampleString {
         get {
             return this.ExampleString;
         }
         set {
             ExampleString = value;
         }
     }
     static public void SerializeToXML(ExampleScriptSave exampleScriptSave)
 {
   XmlSerializer serializer = new XmlSerializer(typeof(ExampleScriptSave));
   TextWriter textWriter = new StreamWriter(@"C:\exampleScriptSave.xml");
   serializer.Serialize(textWriter, exampleScriptSave);
   textWriter.Close();
 }
 
     void OnGUI(){
     if(GUILayout.Button("Save")){
     // no overload for method SerializeToXML takes 0 arguments
     SerializeToXML();
         }
         
     }
 }

Comment
Add comment · Show 1
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 Fattie · Dec 17, 2012 at 08:34 AM 0
Share

just FYI it's possible the amazing (free) Unity Serializer can help you. google "Unity Serializer Whydoidoit"

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Karsnen_2 · Dec 17, 2012 at 04:40 PM

I would prefer Mike's Unity Serializer. But as you have specifically mentioned about values to XML, this link is the best to get a start with.

http://xeophin.net/de/node/1637

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

A node in a childnode? 1 Answer

C# Deserializing XML and Assigning Values 1 Answer

C# moveOnMouseClick Rotation 2 Answers

C# GUI.Button Transform.Position 1 Answer

C# Preserving GameObjects' Previous Meshes 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges