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

Unity3D读取XML文档信息

发布时间:2020-12-16 09:04:55 所属栏目:百科 来源:网络整理
导读:C#中可以用XmlDocument类操作Xml文件 例如要读取如下Xml文件 1 root 2 person name ="WangYao" 3 age 25 / 4 person 5 ="Jobs" 6 56 7 8 程式如下 1 XmlDocument doc = new XmlDocument(); 2 doc.Load( " config.xml " ); // 加载Xml文件 3 XmlElement rootE

C#中可以用XmlDocument类操作Xml文件

例如要读取如下Xml文件

1 <root>  
2   person name="WangYao"3     age>25</4   person5   ="Jobs"6     >567   8 >  

程式如下

1 XmlDocument doc = new XmlDocument(); 2 doc.Load("config.xml"); //加载Xml文件 3 XmlElement rootElem = doc.DocumentElement; 获取根节点 4 XmlNodeList personNodes = rootElem.GetElementsByTagName(person"); 获取person子节点集合 5 foreach (XmlNode node in personNodes) 6 { 7 string strName = ((XmlElement)node).GetAttribute(name"); 获取name属性值 8 Console.WriteLine(strName); 9 XmlNodeList subAgeNodes = ((XmlElement)node).GetElementsByTagName(age"); 获取age子XmlElement集合 10 if (subAgeNodes.Count == 1) 11 { 12 string strAge = subAgeNodes[0].InnerText; 13 Console.WriteLine(strAge); 14 } 15 }

其中XmlElement继承自XmlNode

XmlElement有GetAttribute()&GetElementsByTagName()等方法而XmlNode没有

不管使用XmlNode的ChildNodes属性还是XmlElement的GetElementsByTagName()方法获取的都是XmlNodeList

那这里就存在获取的XmlNodeList中的XmlNode到底是什么类型的问题

可以根据XmlNode的NodeType属性判断

如若等于XmlNodeType.Element就可以强转为XmlElement从而使用XmlElement的方法


转载:http://www.cnblogs.com/Hisin/archive/2012/02/27/2370646.html


Unity3D读取XML文档信息

[csharp] view plain copy
  1. usingSystem;
  2. usingUnityEngine;
  3. usingSystem.IO;
  4. usingSystem.Xml;
  5. usingSystem.Linq;
  6. usingSystem.Text;
  7. usingSystem.Collections.Generic;
  8. namespaceAddress
  9. {
  10. ///<summary>
  11. ///地址数据
  12. ///</summary>
  13. publicclassAddressData
  14. {
  15. ///当前城市ID
  16. staticstring_nowProvinceId;
  17. ///所有省名字
  18. staticList<string>allProvinceName=newList<string>();
  19. ///所有城市id
  20. publicList<string>allCityId=///<summary>
  21. ///所有城市名字
  22. ///</summary>
  23. string>allCityName=string>();
  24. stringlocalUrl=Application.dataPath+"/XMLFile1.xml";
  25. ///加载xml文档
  26. ///<returns></returns>
  27. staticXmlDocumentReadAndLoadXml()
  28. XmlDocumentdoc=newXmlDocument();
  29. Debug.Log("加载xml文档");
  30. doc.Load(localUrl);
  31. returndoc;
  32. }
  33. ///从本地加载xml并获取所有省的名字
  34. ///<paramname="url"></param>
  35. string>GetAllProvinceName()
  36. List<string>_allProvinceName= XmlDocumentxmlDoc=ReadAndLoadXml();
  37. //所有province节点
  38. XmlNodeprovinces=xmlDoc.SelectSingleNode("province");
  39. foreach(XmlNodeprovinceinprovinces)
  40. XmlElement_province=(XmlElement)province;
  41. //所有provinceName添加到列表
  42. allProvinceName.Add(_province.GetAttribute("name"));
  43. }
  44. Debug.Log("所有省数目"+allProvinceName.Count);
  45. _allProvinceName=allProvinceName;
  46. return_allProvinceName;
  47. ///根据当前省ID返回当前省的所有城市名
  48. ///<paramname="nowProvinceId"></param>
  49. string>GetAllCityNameByNowProvinceId(stringnowProvinceId)
  50. List<string>nowAllCityName= XmlDocumentxmlDoc=ReadAndLoadXml();
  51. //所有province节点
  52. XmlNodeprovinces=xmlDoc.SelectSingleNode("province");
  53. inprovinces)
  54. XmlElement_province=(XmlElement)province;
  55. //当前城市id
  56. if(nowProvinceId==_province.GetAttribute("id"))
  57. foreach(XmlElementcityin_province.ChildNodes)
  58. XmlElement_city=(XmlElement)city;
  59. //当前城市的所有cityName添加到列表
  60. nowAllCityName.Add(_city.GetAttribute("name"));
  61. returnnowAllCityName;
  62. ///根据省的ID返回省的名字
  63. ///<paramname="provinceId"></param>
  64. ///<returns></returns>
  65. stringGetProvinceName(stringprovinceId)
  66. string_provinceName="";
  67. if(provinceId==_province.GetAttribute("id"))
  68. //获取实际省名
  69. _provinceName=_province.GetAttribute("name");
  70. return_provinceName;
  71. ///根据城市ID返会城市名字
  72. ///<paramname="cityId"></param>
  73. stringGetCityName(stringcityId)
  74. stringcityName="";
  75. if(_nowProvinceId==_province.GetAttribute("id"))
  76. in_province.ChildNodes)
  77. XmlElement_city=(XmlElement)city;
  78. if(cityId==_city.GetAttribute("id"))
  79. //获取实际城市名
  80. cityName=_city.GetAttribute("name");
  81. returncityName;
  82. }


copy
    usingUnityEngine;
  1. usingSystem.Collections;
  2. usingSystem.Collections.Generic;
  3. usingAddress;
  4. classFinalTest:MonoBehaviour{
  5. //Usethisforinitialization
  6. voidStart()
  7. string>allp= allp=AddressData.GetAllProvinceName();
  8. Debug.Log(AddressData.allProvinceName.Count);
  9. Debug.Log(allp.Count);
  10. string>allCity= allCity=AddressData.GetAllCityNameByNowProvinceId("01");
  11. Debug.Log(allCity.Count);
  12. for(inti=0;i<allCity.Count;i++)
  13. Debug.Log(allCity[i]);
  14. stringa=AddressData.GetProvinceName("02");
  15. Debug.Log(a);
  16. //Updateiscalledonceperframe
  17. voidUpdate()
  18. }


[html] copy
    <?xmlversion="1.0"encoding="utf-8"?>
  1. <province>
  2. provinceid="01"name="江苏"cityid="01"name="南京"></city>
  3. cityid="02"name="镇江"cityid="03"name="南通"provinceid="02"name="河南"cityid="01"name="郑州"cityid="02"name="开封"cityid="03"name="洛阳">
转载:http://blog.csdn.net/awnuxcvbn/article/details/9298395

(编辑:李大同)

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

    推荐文章
      热点阅读