加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

unity 读取xml 信息

发布时间:2020-12-16 09:05:04 所属栏目:百科 来源:网络整理
导读:1、首先在unity工程中Resources/XML目录下创建xml文件:xmls.xml; 如: ?xml version ="1.0" encoding = "utf-8"?rootparent name ="Lily"child name ="L01"123/childchild name ="L02"apple/childchild name ="L03"大/child/parent/root 2、定义我们需要的

1、首先在unity工程中Resources/XML目录下创建xml文件:xmls.xml;

如:

<?xml version ="1.0" encoding = "utf-8"?>
<root>
	<parent name ="Lily">
		<child name ="L01">123</child>
		<child name ="L02">apple</child>
		<child name ="L03">大</child>
	</parent>
</root>

2、定义我们需要的变量:

using System.Xml;

private XmlDocument xmldoc;
private XmlNode root;
private string url;

3、在Start()函数进行初始化:

初始化方法一:

url = Application.dataPath + "/Resources/XML/xmls.xml";
xmldoc = new XmlDocument();
xmldoc.Load(url);
root = xmldoc.SelectSingleNode("root");

初始化方法二:

url = "XML/xmls";
xmlAsset = Resources.Load<TextAsset>(url);
xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlAsset.text);

初始化完成之后、来写我们需要的方法:

4、读取Lily:

void ReadLily()
	{
		XmlElement parent = (XmlElement)root.SelectSingleNode("parent");
		Debug.Log(parent.Name + "Name:" + parent.GetAttribute("name"));
	}

5、读取所有子节点的值:

void ReadAllChildName()
	{
		XmlNode parent = root.SelectSingleNode("parent");
		XmlNodeList childs = parent.SelectNodes("child");
		foreach (XmlNode temp in childs)
		{
			XmlElement ele = (XmlElement)temp;
			Debug.Log(ele.Name + "Name:" + ele.GetAttribute("name") + "    value:" + ele.InnerText);
		}
	}

这样就可以在完成初始化的时候进行调用;

结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读