Unity3D读取XML文档信息
发布时间:2020-12-16 00:38:58 所属栏目:百科 来源:网络整理
导读: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 ///当前城
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() { } }
<?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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |