Unity3D 读取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);
}
}
}
yieldreturn tempText;
// print(tempText);
}
else
{
yield return "www not exists!";
}
}
}xml内容:**** Hidden Message ***** {:5_424:}{:5_424:}{:5_424:}
感谢楼主的无私分享!{:soso__11402694654016840197_7:}
不错 不错 不错{:soso__3922851084632044791_6:} 感谢楼主的无私分享!
不错 不错 不错{:soso__3922851084632044791_6:}
膜拜中。。。。{:soso__7524161091986203637_5:} {:5_427:}谢谢楼主分享,真是受益良多啊
不错 不错 不错{:soso__3922851084632044791_6:} 学习学习