0
\$\begingroup\$

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>));
StreamReader sr = new StreamReader(path);
List<Name> listnames = (List<Name>)xmlSerializer.Deserialize(sr);

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

PS: It is C# , Unity.

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

2 Answers 2

1
\$\begingroup\$

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
{
    [MenuItem ("AssetDatabase/LoadAssetExample")]
    static void ImportExample ()
    {
        Texture2D t = AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg", typeof(Texture2D)) as Texture2D;
    }
}

AssetDatabase.LoadAssetAtPath is the thing you need to use.

\$\endgroup\$
4
  • \$\begingroup\$ 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 \$\endgroup\$
    – auhan
    Nov 21, 2015 at 18:52
  • \$\begingroup\$ How to get the textasset: TextAsset asset = AssetDatabase.LoadAssetAtPath("Assets\Layout\XML\file" + select.ToString() + ".xml"); \$\endgroup\$
    – Almo
    Nov 21, 2015 at 18:54
  • \$\begingroup\$ 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. \$\endgroup\$
    – Elva
    Nov 21, 2015 at 22:10
  • \$\begingroup\$ Right, I had just copied his code. My mistake. \$\endgroup\$
    – Almo
    Nov 21, 2015 at 22:34
0
\$\begingroup\$

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);
    BinaryReader br = new BinaryReader(s);

    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Name>));
    StreamReader sr = new StreamReader(s);
    List<Name> listnames = (List<Name>)xmlSerializer.Deserialize(sr);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .