- 最后登录
- 2018-5-18
- 注册时间
- 2013-5-24
- 阅读权限
- 20
- 积分
- 197
- 纳金币
- 0
- 精华
- 0
|
pstatus"> 本帖最后由 艾西格亚 于 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!";
}
}
}
|
|