unity 解析 XML

Posted projectdeveloping

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity 解析 XML相关的知识,希望对你有一定的参考价值。

代码部分

    /// <summary>
    /// 读取大区和小区
    /// </summary>
    /// <returns></returns>
    public static List<Server> Get_All_Sevrers()
    {
        List<Server> servers = new List<Server>();

        try
        {
            //获取 XML 文件                                     路径正确
            TextAsset textAsset =  Resources.Load<TextAsset>("file\Server");
            //加载 XML 文件内容        
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(textAsset.text);
            //获取根节点        
            XmlNode node = xml.SelectSingleNode("Root");
            //获取子节点        
            XmlNodeList list = node.ChildNodes;
            //遍历解析文件          
            foreach (XmlElement item in list)
            {
                string type = item.Attributes["type"].Value;
                string Server_name = item.Attributes["name"].Value;
                string pictures_Name = item.Attributes["pictures_Name"].Value;
                Dictionary<string, User_Server> valuePairs = Get_User_Server(item, Server_name);

                servers.Add(new Server(type,Server_name,pictures_Name, valuePairs));
            }
        }
        catch (System.Exception)
        {
            Debug.Log("暂时没有文件");
        }
        return servers;
    }

    /// <summary>
    /// 获取小区
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static Dictionary<string, User_Server> Get_User_Server(string name)
    {
        Dictionary<string, User_Server> valuePairs = new Dictionary<string, User_Server>();

        try
        {
            //获取 XML 文件        
            string text = GetText("Server.xml");
            //加载 XML 文件内容        
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(text);
            //获取根节点        
            XmlNode node = xml.SelectSingleNode("Root");
            //获取子节点        
            XmlNodeList list = node.ChildNodes;
            //遍历解析文件            
            foreach (XmlElement item in list)
            {
                string name1 = item.Attributes["name"].Value;
                if (name1 == name)
                {
                    XmlNodeList xml_name = item.ChildNodes;
                    foreach (XmlElement item2 in xml_name)
                    {
                        string type = item2.Attributes["type"].Value;
                        string Server_name = item2.Attributes["Server_name"].Value;
                        string pictures_Name = item2.Attributes["pictures_Name"].Value;
                        string peoNum = item2.Attributes["peoNum"].Value;

                        User_Server data = new User_Server(type, Server_name, pictures_Name, peoNum);
                        valuePairs.Add(Server_name,data);
                    }
                }
            }
        }
        catch (System.Exception)
        {
            Debug.Log("没有数据");
        }

        return valuePairs;
    }

    /// <summary>
    /// 获取小区2
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static Dictionary<string, User_Server> Get_User_Server(XmlElement item,string name)
    {
        Dictionary<string, User_Server> valuePairs = new Dictionary<string, User_Server>();

        try
        {
            string name1 = item.Attributes["name"].Value;
            if (name1 == name)
            {
                XmlNodeList xml_name = item.ChildNodes;
                foreach (XmlElement item2 in xml_name)
                {
                    string type = item2.Attributes["type"].Value;
                    string Server_name = item2.Attributes["Server_name"].Value;
                    string pictures_Name = item2.Attributes["pictures_Name"].Value;
                    string peoNum = item2.Attributes["peoNum"].Value;

                    User_Server data = new User_Server(type, Server_name, pictures_Name, peoNum);
                    valuePairs.Add(Server_name, data);
                }
            }
        }
        catch (System.Exception)
        {
            Debug.Log("没有数据");
        }

        return valuePairs;
    }

XML文件

<?xml version="1.0" encoding="utf-8" ?>

<Root>
  <Server type="大区" name="西北" pictures_Name="btn_火爆2">

    <User_Server type="小区" Server_name="陕西" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="甘肃" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="新疆" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="青海" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="宁夏" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>

  </Server>
  <Server type="大区" name="西南" pictures_Name="btn_火爆2">

    <User_Server type="小区" Server_name="四川" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="重庆" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="贵州" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="云南" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="西藏" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>

  </Server>
  <Server type="大区" name="华南" pictures_Name="btn_火爆2">

    <User_Server type="小区" Server_name="广东" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="广西" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="海南" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="福建" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>

  </Server>
  <Server type="大区" name="华中" pictures_Name="btn_火爆2">

    <User_Server type="小区" Server_name="湖北" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="湖南" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="河南" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="江西" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>

  </Server>
  <Server type="大区" name="东北" pictures_Name="btn_流畅2">

    <User_Server type="小区" Server_name="辽宁" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="吉林" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="黑龙江" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>

  </Server>
  <Server type="大区" name="华北" pictures_Name="btn_流畅2">

    <User_Server type="小区" Server_name="北京" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="天津" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="河北" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="山西" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="内蒙古" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>

  </Server>
  <Server type="大区" name="华东" pictures_Name="btn_火爆2">

    <User_Server type="小区" Server_name="上海" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="江苏" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="浙江" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="山东" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>
    <User_Server type="小区" Server_name="安徽" pictures_Name="btn_火爆2" peoNum ="1000"></User_Server>

  </Server>
</Root>

 

以上是关于unity 解析 XML的主要内容,如果未能解决你的问题,请参考以下文章

从流输入中解析没有根元素的 XML 片段列表

无法解析片段中的 findViewById [重复]

jsoup解析xml某片段的问题

用于 Unity 对话系统目的的 XML 解析具有属性的嵌套对象

Unity中用Mono插件解析xml文件

Unity中实现解析XML文件