Unity(二十三) 在StreamingAssets下讀取XML

    private string name1;
    private string name2;
    private string year1;
    private string year2;
    private string id1;
    private int id2;

public  IEnumerator load()
    {
        string path = string.Empty;
        string line1 = string.Empty;
        if (Application.platform == RuntimePlatform.Android)
        {
            // path = Application.streamingAssetsPath + "/aaa.xml"; 
            path = "jar:file://" + Application.dataPath + "!/assets/" + "aaa.xml";

            WWW wWA = new WWW(path);///WWW讀取在各個平臺上都可使用
            yield return wWA;
            line1 = wWA.text;
           
           
        }
        else
        {
            // path = "file://" + Application.streamingAssetsPath + "/aaa.xml";
            path = Application.dataPath + "/StreamingAssets" + "/aaa.xml";
            WWW wWA = new WWW(path);
            yield return wWA;
            line1 = wWA.text;
            Debug.Log(line1);

            XmlDocument xml = new XmlDocument();

            //跳過BOM 
            System.IO.StringReader stringReader = new System.IO.StringReader(line1);
            stringReader.Read();
            string result = stringReader.ReadToEnd();
            //關閉
            stringReader.Close();


            xml.LoadXml(result);
            XmlNodeList node = xml.SelectSingleNode("item").ChildNodes;
            foreach (XmlElement ele in node)
            {
                //item下面的節點
                Debug.Log(ele.Name);

                if (ele.Name == "item1")
                {
                    //first item1
                    foreach (XmlElement i1 in ele.ChildNodes)
                    {
                        Debug.Log(i1.Name);
                        if (i1.Name == "id")
                        {
                            id1 = i1.InnerText;
                        }
                        if (i1.Name == "name")
                        {
                            name1 = i1.InnerText;
                        }
                        if (i1.Name == "year")
                        {
                            year1 = i1.InnerText;
                        }
                    }
                }
                if (ele.Name == "item2")
                {
                    //first item1
                    foreach (XmlElement i2 in ele.ChildNodes)
                    {
                        Debug.Log(i2.Name);
                        if (i2.Name == "id")
                        {
                            id2 = int.Parse(i2.InnerText);
                        }
                        if (i2.Name == "name")
                        {
                            name2 = i2.InnerText;
                        }
                        if (i2.Name == "year")
                        {
                            year2 = i2.InnerText;
                        }
                    }
                }

            }

          
        }
        yield return null;
    }

 

XML文件

<item>
  <item1>
    <id>1</id>
    <name>china</name>
    <year>2016</year>
  </item1>
  <item2>
    <id>2</id>
    <name>usa</name>
    <year>2017</year>
  </item2>
</item>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章