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 Geoki · Aug 17, 2015 at 08:51 PM · c#unity 5serializationsaveload

Coding help: How to use xml serialization

Hello, I've seen that xml serialization is a very common way to save files and I want to use this because: -System.Runtime.Serialzation.Binary.Formatters doesn't allow to serialize on Windows Phone -PlayerPrefs is very good but can't be used for lists.

So let's get to the point: I want to save this list with xml: public List<string> Collected = new List<string>();

Could anyone help me do this or at least give me a simple example on how to save/load my list. I've looked on unity wiki, google it, search everywhere but still I can't understand the logic it uses as I am new to programming

Thank You!

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

Answer by cjdev · Aug 17, 2015 at 09:10 PM

There are two things you'll need here, the creation of the xml document from the list and then the serialization of the document. I'd recommend using the XDocument and XElement classes and putting the xml file in your Resources folder.

     using System.Collections.Generic;
     using System.Xml.Linq;

     //How to create and populate a new xml document from a list
     XDocument doc = new XDocument();
     doc.Add(new XElement("Items"));
     foreach (string item in Collected)
     {
         doc.Element("Items").Add(new XElement("Item", item));
     }

This will give you an xml document with the following format:

 <?xml version="1.0" encoding="utf-8"?>
 <Items>
     <Item>CollectedValue1</Item>
     <Item>CollectedValue2</Item>
     <Item>CollectedValue3</Item>
     <Item>CollectedValue4</Item>
 ...
 </Items>

And finally, once you have your xml structured you can read and write it to a file as you like:

     //This is how you load a xml document from your Resources folder
     TextAsset xmlText = Resources.Load("CollectedXML") as TextAsset;
     XDocument myData = XDocument.Parse(xmlText.text);

     //And this is how you write it
     myData.Save(Application.dataPath + "/Resources/CollectedXML.xml");
Comment
Add comment · Show 9 · 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 Geoki · Aug 18, 2015 at 09:28 AM 0
Share

Thank you for your quick reply! But after that how I can apply the xml document's content to the list "collected"?

avatar image cjdev · Aug 18, 2015 at 09:38 AM 0
Share

You can do this:

 List<XElement> Collected = doc.Descendants("Item").ToList();

Also check out LINQ to X$$anonymous$$L for more sorting and searching functions you can do with your XElement objects.

avatar image Geoki · Aug 18, 2015 at 10:15 AM 0
Share

Gives me this error:

Assets/Scripts/GameController.cs(69,72): error CS1061: Type System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for ToList' and no extension method ToList' of type System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

Probably there isn't a definition for "ToList"

avatar image cjdev · Aug 18, 2015 at 10:24 AM 0
Share

Sorry, you'll need to add using System.Linq; for that.

Also I just saw you'd probably want the Collected list in a string type so you could use a bit of LINQ to do that like this:

 doc.Descendants("Item").Select(x => x.Value).ToList();
avatar image Geoki · Aug 18, 2015 at 10:34 AM 0
Share

Either I am messing something or it's just to difficult I have given all my code below... I don't know what I did wrong :/

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Collections.Generic;
 using System.Xml;
 using System.Xml.Serialization;
 using System.IO;
 using System.Xml.Linq;
 using System.Linq;
 
 public class GameController : $$anonymous$$onoBehaviour {
 
     public static GameController Controller;
     public int Lives;
     public int Wumpas;
     public int Health;
     
     // Use this for initialization
     void Awake () {
         if (Controller == null) {
             DontDestroyOnLoad (gameObject);
             Controller = this;
         }
         else if (Controller != null) {
             Destroy(gameObject);
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void Save() {
         PlayerPrefs.SetInt ("Wumpas", Wumpas);
         PlayerPrefs.SetInt ("Lives", Lives);
         PlayerPrefs.SetInt ("Health", Health);
         $$anonymous$$akeDocument ();
     }
 
     public void Load() {
         Wumpas = PlayerPrefs.GetInt ("Wumpas");
         Lives = PlayerPrefs.GetInt ("Lives");
         Health = PlayerPrefs.GetInt ("Health");
         Load2();
     }
     //public List<string> Uncollected = new List<string>();
     public List<string> Collected = new List<string>();
 
     public void Earned(string name) {
         //Uncollected.Remove(name);
         Collected.Add(name);
     }
 
     public void $$anonymous$$akeDocument() {
         //How to create and populate a new xml document from a list
         XDocument doc = new XDocument ();
         doc.Add (new XElement ("Items"));
         foreach (string item in Collected) {
             doc.Element ("Items").Add (new XElement ("Item", item));
         }
         //doc.Save(Application.dataPath + "/CollectedX$$anonymous$$L.xml");
         doc.Save(Application.dataPath + "/Resources/CollectedX$$anonymous$$L.xml");
         Debug.Log ("File saved on: " + Application.dataPath + "/Resources/CollectedX$$anonymous$$L.xml");
     }
     public void Load2() {
         //TextAsset xmlText = Resources.Load("CollectedX$$anonymous$$L") as TextAsset;
         TextAsset xmlText = Resources.Load ("CollectedX$$anonymous$$L") as TextAsset;
         XDocument myData = XDocument.Parse(xmlText.text);
     }
 
     public void Save2() {
         XDocument myData = new XDocument();
         myData.Save(Application.dataPath + "/Resources/CollectedX$$anonymous$$L.xml");
     }
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Save and load serialization problem 0 Answers

Can't get serialization Load/Save to work!C# 3 Answers

My save function saves the wrong values!! 1 Answer

Unity Saved Serialized Levels 0 Answers

How do I save and load the value of a text? 1 Answer


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