纳金网

标题: 【脚本资源】 Unity3D 读取XML中的内容代码 [打印本页]

作者: lp5131427    时间: 2013-5-29 17:33
标题: 【脚本资源】 Unity3D 读取XML中的内容代码
本帖最后由 艾西格亚 于 2013-5-30 01:31 编辑

前阵子做WebServer的时候读取服务器返回的XML研究好久··· 今天刚刚看到有人发了教程所以就发上来

using System.Xml;
using System.IO;
using LitJson;
public string showXml(string temp)
    {
        UnityEditor.AssetDatabase.Refresh();
        string filepath = Application.dataPath + "/my.xml";
        string tempText = "";
        print("a");
        print(File.Exists(filepath).ToString());
        if (File.Exists(filepath))
        {
            print("b");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);//若要读网络 前面改成IEnum线程这里改网络xml地址即可
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("main").ChildNodes;
            //
            foreach (XmlElement xe in nodeList)
            {
             //   Debug.Log(xe.GetAttribute("name"));
             //   Debug.Log(xe.Name);
                foreach (XmlElement x1 in xe.ChildNodes)
                {
                    if (x1.Name == temp)
                    {
                        tempText= x1.InnerText;
                        //    print(x1.InnerText);
                    }
                }

            }
            return tempText;
            print(tempText);
        }
        else
        {
            return "file not exists!";
        }
      //  return "";
    }

网络版:

IEnumerator showXmls(string temp,UILabel tempLabel)
    {
        WWW www = new WWW("http://zg.0099.com/resources/my.xml");
        yield return www;
        if(www!=null)
        {
            string tempText = "";
            if (www!=null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("http://zg.0099.com/resources/my.xml");
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("main").ChildNodes;
                //
                foreach (XmlElement xe in nodeList)
                {
                    //   Debug.Log(xe.GetAttribute("name"));
                    //   Debug.Log(xe.Name);
                    foreach (XmlElement x1 in xe.ChildNodes)
                    {
                        if (x1.Name == temp)
                        {
                            tempText = x1.InnerText;
                            tempLabel.text = x1.InnerText;
                          //  (UILabel)tempText = x1.InnerText;
                            //    print(x1.InnerText);
                        }
                    }

                }
              yield  return tempText;
               // print(tempText);
            }
            else
            {
                yield return "www not exists!";
            }
        }
    }



作者: 狂风大尉    时间: 2013-5-30 16:06
顶一个,果断的先收藏下来
作者: libufan    时间: 2013-6-1 00:23
谢谢楼主分享




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5