Unity解析XML 《二》

unity 在编辑器模式下可以使用序列化和反序列化的方式解析XML
但是如果到了IOS平台,使用反序列化的可能会导致错误,
建议使用解析XML的方式

xml文件如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<item ID="1">
		<videoName>north_and_south</videoName>
		<vid>c617df497466e5c60ca484ad9e0195b1_c</vid>
	</item>
	<item ID="2">
		<videoName>yangtze_river_delta</videoName>
		<vid>c617df497426087edda25545b6f74f47_c</vid>
	</item>
	<item ID="3">
		<videoName>xinjiang_geography</videoName>
		<vid>c617df4974e3d5453828f8cb9f57924d_c</vid>
	</item>
	<item ID="4">
		<videoName>taiwan_economy</videoName>
		<vid>c617df4974aaebf8879cb13b5abce028_c</vid>
	</item>
</root>

解析代码:

  public static Dictionary<string, string>  ParseVidCfg(string path,string rootName)
    {
        Dictionary<string, string> vidDic = new Dictionary<string, string>();

        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        XmlNodeList nodlist = doc.SelectSingleNode(rootName).ChildNodes;

        for(int i = 0; i < nodlist.Count; i++)
        {
            XmlNodeList nodeList= nodlist[i].ChildNodes;
            string vid = string.Empty;
            string videoName = string.Empty;
            for (int j = 0; j < nodeList.Count; j++)
            {             
                switch (nodeList[j].Name)
                {
                    case "videoName":
                        videoName = nodeList[j].InnerText;
                    break;
                    case "vid":
                        vid = nodeList[j].InnerText;
                        break;
                }                                
            }

            if(!string.IsNullOrEmpty(vid) && !string.IsNullOrEmpty(videoName))
            {
                vidDic.Add(videoName,vid);
            }
        }



        return vidDic;
    }

调用代码

 public Dictionary<string, string> vidDic = new Dictionary<string, string>();
 vidDic= LoadXML.ParseVidCfg(FileServer.Instance.PathDeal(ConstConfig.vidCfgPath), "root");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值