Unity3D读取XML文档信息

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

相关文章

前言 本文记录unity3D开发环境的搭建 unity安装 unity有中文...
前言 有时候我们希望公告牌跟随镜头旋转永远平行面向屏幕,同...
前言 经过一段时间的学习与实际开发,unity3D也勉强算是强行...
前言 在unity中我们常用的获取鼠标点击的方法有: 1、在3D场...
前言 在之前的例子中,我们都没有用到unity的精髓,例如地形...
这篇文章将为大家详细讲解有关Unity3D中如何通过Animator动画...