LocationParseComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System.Xml;
  6. namespace JC.Unity.Picker {
  7. public class LocationParseComponent : MonoBehaviour {
  8. [SerializeField] TextAsset xmlFileText;
  9. LocationParser locationParser;
  10. public static LocationParseComponent ins;
  11. void Awake() {
  12. ins = this;
  13. locationParser = new LocationParser(xmlFileText);
  14. }
  15. void OnDestroy() {
  16. if (ins == this) ins = null;
  17. }
  18. public Tuple<string, string, string> ParseNameByCode(string countryCode, string stateCode, string cityCode) {
  19. string countryName = "";
  20. string stateName = "";
  21. string cityName = "";
  22. XmlNodeList stateNodeList = null;
  23. XmlNodeList cityNodeList = null;
  24. foreach (var countryNode in locationParser.CountryRegionList) {
  25. if (countryNode.Attributes["Code"].Value == countryCode) {
  26. countryName = countryNode.Attributes["Name"].Value;
  27. stateNodeList = countryNode.SelectNodes("State");
  28. break;
  29. }
  30. }
  31. if (stateNodeList != null) {
  32. for (int i = 0; i < stateNodeList.Count; i++) {
  33. XmlNode stateNode = stateNodeList[i];
  34. XmlAttribute CodeAtb = stateNode.Attributes["Code"];
  35. if (CodeAtb != null) {
  36. if (stateCode == CodeAtb.Value) {
  37. XmlAttribute NameAtb = stateNode.Attributes["Name"];
  38. if (NameAtb != null) {
  39. stateName = NameAtb.Value;
  40. cityNodeList = stateNode.SelectNodes("City");
  41. break;
  42. }
  43. }
  44. } else if (CodeAtb == null && string.IsNullOrEmpty(stateCode)) {
  45. cityNodeList = stateNode.SelectNodes("City");
  46. break;
  47. }
  48. }
  49. }
  50. if (cityNodeList != null) {
  51. for (int i = 0; i < cityNodeList.Count; i++) {
  52. XmlNode cityNode = cityNodeList[i];
  53. XmlAttribute CodeAtb = cityNode.Attributes["Code"];
  54. if (CodeAtb != null) {
  55. if (cityCode == CodeAtb.Value) {
  56. XmlAttribute NameAtb = cityNode.Attributes["Name"];
  57. if (NameAtb != null) {
  58. cityName = NameAtb.Value;
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. return new Tuple<string, string, string>(countryName, stateName, cityName);
  66. }
  67. }
  68. }