Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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.

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# -- Build character unit from script 1 Answer

C# How to Drag and Scale with Mouse Window 0 Answers

C# How to Check If a Gameobject's tag doesn't equal a Specific Tag 3 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