# Reading XML working in my computer but not in my Android phone

Following code works perfect in my computer but when I try to run it in my Android Phone, it does not work.

string path = @"Assets\Layout\XML\file" + select.ToString() + @".xml";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Name>));
List<Name> listnames = (List<Name>)xmlSerializer.Deserialize(sr);


How can I make the code work in my Android Phone?

PS: It is C# , Unity.

• Which language? Which framework? I'm guessing C# and .NET, but inquiring minds want to know!
– Almo
Nov 21, 2015 at 15:42
• Ow, I am sorry, I forgot to say it. It is C# @Almo Nov 21, 2015 at 15:42
• Unity? Looks like it might be Unity. If so, I might be able to help.
– Almo
Nov 21, 2015 at 15:44
• Yes, it is Unity. @Almo Nov 21, 2015 at 15:47
• May be you are hardcoding the path like "C:\" or "D:\", which prevents to find file on android. Nov 21, 2015 at 16:02

What you are trying to do is "load an asset". Here's how:

http://docs.unity3d.com/Manual/AssetDatabase.html

using UnityEngine;
using UnityEditor;

public class ImportAsset
{
static void ImportExample ()
{
Texture2D t = AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg", typeof(Texture2D)) as Texture2D;
}
}


AssetDatabase.LoadAssetAtPath is the thing you need to use.

• I have tried to write in many ways but I could not make it work. If you have a time, can you explain it with the same name I used in my first code? @Almo Nov 21, 2015 at 18:52
• How to get the textasset: TextAsset asset = AssetDatabase.LoadAssetAtPath("Assets\Layout\XML\file" + select.ToString() + ".xml");
– Almo
Nov 21, 2015 at 18:54
• Don't use , the only operating system that uses that is windows, neither ios nor android will support it. Use / which is universally supported in modern operating systems.
– Elva
Nov 21, 2015 at 22:10
• Right, I had just copied his code. My mistake.
– Almo
Nov 21, 2015 at 22:34

I almost gave up but finally solved it. Unity converts everything to binary variable so that the first thing you need to do is to convert all xml files you have to txt and copy them to "Assets/Resources" folder.

Then, you can use following code:

    TextAsset asset = Resources.Load("file" + select.ToString()) as TextAsset;
Stream s = new MemoryStream(asset.bytes);