Unity 读取Xml

使用 Untiy读取XML,读取成功关键在与17~19这三行,一定要跳过BOM,要不然就一直获取不到值。

IEnumerator LoadXml( string filePath, string imageName)
    {
        WWW www = new WWW(filePath);
        yield return www;
        //Debug.Log(www.text);

        if (www.error != null)
        {
            print("The request failed");
        }
        else
        {
            print("The request is successful");
            //创建
            XmlDocument XmlDoc = new XmlDocument();
            //跳过BOM 
            System.IO.StringReader stringReader = new System.IO.StringReader(www.text);
            stringReader.Read();
            string result = stringReader.ReadToEnd();
            //关闭
            stringReader.Close();

            //加载文本
            XmlDoc.LoadXml(result);

            //获取节点个数
            int XmlCount = XmlDoc.GetElementsByTagName("JD").Count;

            for (int i = 0; i < XmlCount; i++)
            {
                if (XmlDoc.GetElementsByTagName("Name")[i].InnerText == imageName)
                {
                    string Name = XmlDoc.GetElementsByTagName("Name")[i].InnerText;
                    string Path = XmlDoc.GetElementsByTagName("Path")[i].InnerText;

                }
            }
        }
    }

 

XML文本



  
    A
    http://-------
  
  
    B
    http://-------
  

 

你可能感兴趣的:(Xml)