unity获取xml文件内容

我是将xml放在了unity Resources文件夹下,如图
在这里插入图片描述
然后在xml写下你要获取指定物体得内容
在这里插入图片描述
接下来就是脚本上如何获取了
Dictionary<int, string> dicxml;
public int dicindex = 0;
XmlNodeList node;
XmlDocument xmlDoc;

//解析xml
void parseXml()
{
filePath = Application.dataPath + “/Resources/item.xml”;
if (File.Exists(filePath))
{
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
node = xmlDoc.SelectSingleNode(“item”).ChildNodes;

        for (int i = 0; i < node.Count; i++)
        {
            dicxml.Add(dicindex++, node[i].Name);

        }
    }
}
//通过点击UI获取物体名称,这个函数我是挂在了button上面

public void Room_Introduces()
{
var button = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;
string buttonname = button.name;
Debug.Log(buttonname);
#region xml获取物体名称
if (dicxml.ContainsValue(button.name))
{
Debug.Log(buttonname+"_________________");
XmlElement xmlElem = xmlDoc.DocumentElement;//获取根节点
XmlNodeList xnl = xmlElem.GetElementsByTagName(button.name);//取节点名
for (int i = 0; i < xnl.Count; i++)
{
foreach (XmlElement i1 in xnl[i].ChildNodes)
{
bhit = true;
if (i1.Name == “name”)
{
name1 = i1.InnerText;
Debug.Log(name);
}
if (i1.Name == “id”)
{
id1 = i1.InnerText;
}
if (i1.Name == “year”)
{
year = i1.InnerText;
Debug.Log(year);
}
}
}
}
else
{
Debug.Log(“已存在”);
}
#endregion
}

写的不好,请多见谅,有问题留言

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值