Read the XML file after Unity Android is packaged

Source: Internet
Author: User

Original address:

Question: The day before yesterday in the process of doing things found a very tangled problem, why the Unity program on the PC to test a little bit, but the package was released to Android but unable to read the XML file.

By finding the path that was not sent to Android after the package was found by the data discovery and the paths on the PC were changed, so the read was out of the bug.

Then the workaround is simple:

1, set up a new project

2, add two Guitext components one for displaying the test platform another to display the read XML data,

As follows:

3, that's the code.

------------------------------------------------------------------------------//<auto-generated>// This code is generated by the tool. Runtime version: 4.0.30319.18063////changes to this file may result in incorrect behavior, and if//regenerate code, these changes will be lost. </auto-generated>//------------------------------------------------------------------------------using System;using unityengine;using;using system.xml;using system.linq;using System.Text;using        System.collections;using system.collections.generic;namespace assemblycsharp1{public class AddressData1 {        public string Timeurl;        public static string all;                public static string HP;        public static string speed;                public static string Demage;        public static string LocalPath;        public static string ID;        public static string score;        public static list<int> Allscore;        public void Addressdata () {Debug.Log (LocalPath); } public static list<int> GetallscoRe () {return allscore; }///<summary>//Get XML Path///</summary>//<returns>the xm.</returns&        Gt                public static IEnumerator GetXML () {if ( { LocalPath = application.streamingassetspath+ "/score.xml";            Instantiating www in Android cannot precede the path with "file://" Debug.Log (LocalPath); } else {LocalPath = "file://" +unityengine.application.streamingassetspath + "/score.            XML ";//instantiation of WWW in Windows must precede the path with" file://"Debug.Log (LocalPath);            www www = new www (localPath);                while (!www.isdone) {Debug.Log ("Getting GetXML");                yield return www;                all = Www.text;            Parsexml (WWW); }}///<summary>///By attributes///</summary>//<param name= "www" >Www.</param> public static void Parsexml (www www) {            if (Allscore = = null) {Allscore =new list<int> ();            } XmlDocument xmldoc = new XmlDocument ();            Xmldoc.loadxml (Www.text); XmlNodeList Nodelist=xmldoc.selectsinglenode ("Rank").                        ChildNodes; foreach (XmlElement xe in nodeList) {id = XE.                GetAttribute ("id"); Score = XE.                GetAttribute ("Score"); Allscore.add (int.  Parse (score));            Read all scores into List Debug.Log ("ID:" +id+ "score:" +score);            } allscore.sort ();            Allscore.reverse (); foreach (Var score in Allscore) {Debug.Log (score.            ToString ()); }}///<summary>//Read XML content///</summary> public static Ienumera      Tor load () {      String url = string.            Empty; String path = string.            Empty; String line1 = String.            Empty; if ( {url=application.streamingassetspath+ "/hp.xml ";                Instantiating www in Android cannot precede the path with "file://" www wwa=new www (path);///www reads are available on all platforms                 Yield return WWA;                Line1=wwa.text;            Debug.Log (line1); } else {url= "file://" +application.streamingassetspath+ "/hp.xml";//instantiation of WWW in Windows must be                In front of the path add "file://" www wwa=new www ("file://" +url);                 Yield return WWA;                Line1=wwa.text;            Debug.Log (line1);        } yield return null;        }///<summary>//Load XML documents///</summary>//<returns></returns> public static XmlDocument Readandloadxml () {XmlDocument doc = new XmlDocument ();            Debug.Log ("Load XML document"); Doc.            Load (LocalPath);        return doc; }///<summary>//Add nodes///</summary>//<returns>the NODE.&LT;/RETURNS&G        T  <param name= "score" >Score.</param> public static void Insertnode (int score) {int Minute=int.            Parse ((System.DateTime.Now.Minute.ToString ()));            String order = "" "";            XmlDocument xmldoc=new XmlDocument ();            Xmldoc.load (Application.datapath + "/streamingassets/score.xml");                        XmlNode Root=xmldoc.selectsinglenode ("rank");    XmlElement xel=xmldoc.createelement ("rank"); establishes the node Xel.            SetAttribute ("id", order); Xel. SetAttribute ("Score", score.                        ToString ()); Root.            AppendChild (Xel); (Application.datapath + "/Streamingassets/score.xml ");        Return }    }}

Test read Data code:

Using unityengine;using system.collections;using Assemblycsharp1;public class Testinsert:monobehaviour {public    Guitext Guitext;    public Guitext platform;    String allscores= "";    void Awake ()    {        if (        {            Platform.text = "Android";        }        else        {            Platform.text = "PC";        }    }    Use the this for initialization    void Start ()     {        startcoroutine (Addressdata1.getxml ());        Addressdata1.insertnode (a);    }    void Ongui ()    {        if (GUI. button (new Rect (100,100,40,40), "Load"))        {            foreach (int score in Addressdata1.allscore)            {                Allscores+=score. ToString ();                guitext.text+= "\ t";            }            Guitext.text = allscores;        }    }    Update is called once per frame    void Update ()     {        }}

XML file:

<?xml version= "1.0" encoding= "UTF-8"?><rank> <rank    id= "5618" score= "></rank>    " <rank id= "1712" score= "></rank></rank>"

The time for the frozen chicken is here:
Before PC-side operation:

After running:

Click the load button:

See what the console is outputting:

Test OK on the PC.

Next Android test, my phone oh:

To this end, after the process of data processing is a cloud!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.