unity3D读取XML文档信息

<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() 
    {
	
	}
}


转载于:https://my.oschina.net/u/3015045/blog/810765

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值