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
1
Question by Billy 1 · Apr 20, 2011 at 05:27 AM · xmlresources

Loading XML file from resources file after Build

I have an xml file that stores dialog used for my game. This file is stored inside of the resources folder and is read in by specifying the exact path name: "XmlData\strings"

This works just fine when running through the editor, but when I try to run after I built the project, it doesn't work.

I have found a few solutions that say to append .dll to the .xml file and load it in using dllimport, but I still can't get this to work. Does anyone have a good solution for reading in this .xml file from the built resources directory?

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

6 Replies

· Add your reply
  • Sort: 
avatar image
17

Answer by user-3451 (google) · May 09, 2011 at 10:58 AM

The solution is to use Ressources.Load(filename without extention)

e.g. to load your XML file from inside the Ressource Folder

// if your original XML file is located at
// "Ressources/MyXMLFile.xml"
TextAsset textAsset = (TextAsset) Resources.Load("MyXMLFile");  
XmlDocument xmldoc = new XmlDocument ();
xmldoc.LoadXml ( textAsset.text );

Hope that helped you loading your XML file.

Comment
Add comment · Show 1 · 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 malkere · Mar 16, 2018 at 02:05 PM 0
Share

This worked for me using JsonUtility.FromJson(textAsset.text) which I had generated and saved using a binary formatter, though I had to open my text file and clear the BO$$anonymous$$ heading using notepad++

avatar image
6

Answer by Jean-Fabre · Apr 20, 2011 at 06:02 AM

Hi,

Not really a solution to read from the built resource ( I am not sure if you actually know the difference between the Resource folder and the project resources that are made available automatically by unity).

Check this link first: This might be your issue:

Instead of storing it in the Resource folder or anything fancy, simply declare a public var in your script interested by this xml file:

public var xmlDefinition:TextAsset;

And in the inspector, simply drag and drop the xml file onto the "xmlDefinition" field now available.

Then it will be included during publishing now.

If I misunderstood your problem, can you elaborate more on how you actually have it to work within the editor? what function to you use to read the xml.

Bye,

Jean

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
avatar image
2
Wiki

Answer by methotec · May 31, 2012 at 04:13 PM

Hi there, If you encounter the Utf-8 BOM error (line 1 problem) here might be the solution for you: (the whole static class)

 using UnityEngine;
 using System;
 using System.Xml;
 using System.IO;
 
 [ExecuteInEditMode]
 [Serializable]
 public static class RyXmlTools
 {
     public static XmlDocument loadXml(TextAsset xmlFile)
     {
         MemoryStream assetStream = new MemoryStream(xmlFile.bytes);
         XmlReader reader = XmlReader.Create(assetStream);
         XmlDocument xmlDoc = new XmlDocument();
         try
         {
             xmlDoc.Load(reader);
         }
         catch (Exception ex)
         {
             Debug.Log("Error loading "+ xmlFile.name + ":\n" + ex);
         }
         finally
         {
             Debug.Log(xmlFile.name + " loaded");
         }
 
         return xmlDoc;
     }
 
     public static void writeXml(string filepath, XmlDocument xmlDoc)
     {
         if (File.Exists(filepath))
         {
             using (TextWriter sw = new StreamWriter(filepath, false, System.Text.Encoding.UTF8)) //Set encoding
             {
                 xmlDoc.Save(sw);
             }
         }
     }
 }


Keep in mind that you can only write to the text asset in edit mode - AND that the xml file has to be in resources - so you can load it at runtime... here a short example how to use it:

To load the XmlDocument TextAsset:

 TextAsset m_XmlTextAsset = (TextAsset)Resources.Load("FileNameWhitoutFileExtention", typeof(TextAsset));
 XmlDocument m_xmlDoc = RyXmlTools.loadXml(m_XmlTextAsset);

And to save it:

 private static string m_XmlTextAssetPath = Application.dataPath.ToString() + "/resources/FileName.xml";
 RyXmlTools.writeXml(m_XmlTextAssetPath , m_xmlDoc);


I hope this helps...

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
avatar image
0

Answer by Unamine · May 09, 2011 at 11:56 AM

After the build, put that folder as you said: Resources \ XmlData \ strings in the same directory as the executable or does not work inside the folder: name of the game _data

Hope this helps ..

Please could you pass me the script to read information from an XML? and how to tell the GUI that will read a specific line of XML?

Thanks ...

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
avatar image
0

Answer by Kevin Ambruster · Sep 14, 2011 at 04:27 AM

I'm not 100% sure what your problem is but If what you want to do is load in an xml file after build than you just need to use Application.datapath + "filename.xml". Application.datapath will return the path to the assets folder while in the Editor and the path to the data folder when running a windows build. Also will get you the correct path on whatever other device you build it on.

Resource Folders must be added in the editor and can't be added after build time.

Hope that helped.

Comment
Add comment · Show 1 · 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 Isa · Sep 19, 2011 at 02:49 PM 0
Share

This worked fine for me but only if, after running the build, I copy the xml file and everything else from the project/Assets/Resources folder (some prefabs eg) manually to the data/ Resources folder. What's wrong with my resources folder that files/prefabs aren't included?

  • 1
  • 2
  • ›

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can gameobject be exported as unity3d form? 0 Answers

File extensions other than XML in Resources? 1 Answer

Write to XML-file within Assets\Resources 1 Answer

Different approach to loading XML data 1 Answer

Load xml from resource folder after build(web player) 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