- Home /
Convert XML Deserializer from folder to resources
Currently, I have a system to read an XML file but it doesn't work in the build because the XML file is stored in editor folders. Trying to convert it to resources but I don't fully know how to?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
[XmlRoot ("SocialFeedCollection")]
public class PostListContainer
{
[XmlArray("SocialFeedPosts"), XmlArrayItem("Post")]
public SocialFeedPost[] Posts;
public static PostListContainer Load(string path)
{
var serializer = new XmlSerializer(typeof(PostListContainer));
using (var stream = new FileStream(path, FileMode.Open))
{
return serializer.Deserialize(stream) as PostListContainer;
}
}
}
path is inputted from another class but the rest is in this class.
Answer by ATate · Feb 19, 2021 at 05:35 PM
Create a folder inside Assets folder called "Resources", copy your file into it and follow this: https://docs.unity3d.com/ScriptReference/Resources.Load.html
I think current practice is not to use Resource folders as you can get unused assets compiled in your program later on.
To access a pre-existing file using script use TextAsset class
If you are saving/loading file at runtime you can use: Application.persistentDataPath + "/MyFile.Txt"
This stores in the App's data folder on android/IOS, and in user profile for editor at C:\Users\\AppData\LocalLow\COMPANY\APPNAME
https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
Answer by LMacalister · Feb 23, 2021 at 01:15 PM
Currently, I'm using:
postContainer = PostListContainer.Load(Path.Combine(Application.dataPath, "XML/SocialFeed/SocialFeedPosts.xml"));
This doesn't work in the build as the folder doesn't keep in the build. Are you saying the way you said keeps the folder when built?
The file will already exist and won't change. It gets loaded when a certain level loads to populate a fake social media field
If you want the file to be present after the build and installed on a device I think you want a Strea$$anonymous$$g Asset. I've never used it, but seems to bundle a file in the build and place it into destination file system
Create folder called Strea$$anonymous$$gAssets in Assets folder
Copy XML file to the new folder.
Access in program with Path.Combine(Application.strea$$anonymous$$gAssetsPath, filename)
Hope it helps.
Your answer
Follow this Question
Related Questions
Resources.Load() What has to be in Resources Folder 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
XML parsing c# in unity 1 Answer
Loading XML asset after build 0 Answers