unity3D读取XML文档信息
2016-12-23 00:00
387 查看
<pre name="code" class="plain"><?xml version="1.0" encoding="utf-8"?> <province> <province id ="01" name="江苏"> <city id ="01" name ="南京"></city> <city id ="02" name ="镇江"></city> <city id ="03" name ="南通"></city> </province> <province id ="02" name="河南"> <city id ="01" name ="郑州"></city> <city id ="02" name ="开封"></city> <city id ="03" name ="洛阳"></city> </province> </province>
using System;using UnityEngine;using System.IO;using System.Xml;using System.Linq;using System.Text;using System.Collections.Generic;namespace Address{ /// <summary> /// 地址数据 /// </summary> public class AddressData { /// <summary> ///当前城市ID /// </summary> public static string _nowProvinceId; /// <summary> /// 所有省名字 /// </summary> public static List<string> allProvinceName = new List<string>(); /// <summary> /// 所有城市id /// </summary> public List<string> allCityId = new List<string>(); ///<summary> ///所有城市名字 ///</summary> public List<string> allCityName = new List<string>(); public static string localUrl = Application.dataPath + "/XMLFile1.xml"; /// <summary> /// 加载xml文档 /// </summary> /// <returns></returns> public static XmlDocument ReadAndLoadXml() { XmlDocument doc = new XmlDocument(); Debug.Log("加载xml文档"); doc.Load(localUrl); return doc; } /// <summary> /// 从本地加载xml并获取所有省的名字 /// </summary> /// <param name="url"></param> /// <returns></returns> public static List<string> GetAllProvinceName() { List<string> _allProvinceName = new List<string>(); XmlDocument xmlDoc = ReadAndLoadXml(); //所有province节点 XmlNode provinces = xmlDoc.SelectSingleNode("province"); foreach (XmlNode province in provinces) { XmlElement _province = (XmlElement)province; //所有provinceName添加到列表 allProvinceName.Add(_province.GetAttribute("name")); } Debug.Log("所有省数目"+allProvinceName.Count); _allProvinceName = allProvinceName; return _allProvinceName; } /// <summary> /// 根据当前省ID返回当前省的所有城市名 /// </summary> /// <param name="nowProvinceId"></param> /// <returns></returns> public static List<string> GetAllCityNameByNowProvinceId(string nowProvinceId) { List<string> nowAllCityName = new List<string>(); XmlDocument xmlDoc = ReadAndLoadXml(); //所有province节点 XmlNode provinces = xmlDoc.SelectSingleNode("province"); foreach (XmlNode province in provinces) { XmlElement _province = (XmlElement)province; //当前城市id if (nowProvinceId == _province.GetAttribute("id")) { foreach (XmlElement city in _province.ChildNodes) { XmlElement _city = (XmlElement)city; //当前城市的所有cityName添加到列表 nowAllCityName.Add(_city.GetAttribute("name")); } } } return nowAllCityName; } /// <summary> /// 根据省的ID返回省的名字 /// </summary> /// <param name="provinceId"></param> /// <returns></returns> public static string GetProvinceName(string provinceId) { string _provinceName = ""; XmlDocument xmlDoc = ReadAndLoadXml(); //所有province节点 XmlNode provinces = xmlDoc.SelectSingleNode("province"); foreach (XmlNode province in provinces) { XmlElement _province = (XmlElement)province; if (provinceId == _province.GetAttribute("id")) { //获取实际省名 _provinceName= _province.GetAttribute("name"); } } return _provinceName; } /// <summary> /// 根据城市ID返会城市名字 /// </summary> /// <param name="cityId"></param> /// <returns></returns> public static string GetCityName(string cityId) { string cityName=""; XmlDocument xmlDoc = ReadAndLoadXml(); //所有province节点 XmlNode provinces = xmlDoc.SelectSingleNode("province"); foreach (XmlNode province in provinces) { XmlElement _province = (XmlElement)province; if (_nowProvinceId == _province.GetAttribute("id")) { foreach (XmlElement city in _province.ChildNodes) { XmlElement _city = (XmlElement)city; if (cityId == _city.GetAttribute("id")) { //获取实际城市名 cityName = _city.GetAttribute("name"); } } } } return cityName; } } }
using UnityEngine; using System.Collections; using System.Collections.Generic; using Address; public class FinalTest : MonoBehaviour { // Use this for initialization void Start () { List<string> allp = new List<string>(); allp = AddressData.GetAllProvinceName(); Debug.Log(AddressData.allProvinceName.Count); Debug.Log(allp.Count); List<string> allCity = new List<string>(); allCity = AddressData.GetAllCityNameByNowProvinceId("01"); Debug.Log(allCity.Count); for (int i = 0; i < allCity.Count; i++) { Debug.Log(allCity[i]); } string a = AddressData.GetProvinceName("02"); Debug.Log(a); } // Update is called once per frame void Update() { } }
相关文章推荐
- Unity3D读取XML文档信息实例
- unity3D读取XML文档信息
- Unity3D读取XML文档信息
- Unity3D读取XML文档信息
- unity3D读取XML文档信息
- Unity3D读取XML文档信息
- 根据XML文档读取连接数据库信息
- 读取XML文档解析信息
- 读取xml文档中的信息
- 在asp.net中读取XML文件信息的4种方法(转载)
- 在asp.net中读取XML文件信息的4种方法(ZT)
- C#来创建和读取XML文档
- 用C#来创建和读取XML文档
- asp SQL server 2000 读取记录直接生成xml文档
- 关于用JS读取XML文档的问题
- ASP.NET中读取XML文件信息的4种方法与示例代码
- 利用代码读取XML文档
- 在.NET中使用XML、读取XML文件,插入数据到XML文档中。
- C#读取XML文档
- C#来创建和读取XML文档