XML读取的几种方式

编写一个XML文件如下:

[html] view plain copy
print ?
  1. xml version=“1.0” encoding=“utf-8”?>  
  2. <Xml>  
  3.     <JD>  
  4.         <Name>节点01Name>  
  5.         <X>001X>  
  6.         <Y>002Y>  
  7.     JD>  
  8.     <JD>  
  9.         <Name>节点02Name>  
  10.         <X>003X>  
  11.         <Y>004Y>  
  12.     JD>  
  13.     <JD>  
  14.         <Name>节点03Name>  
  15.         <X>005X>  
  16.         <Y>006Y>  
  17.     JD>  
  18. Xml>  


    
        节点01
        001
        002
    
    
        节点02
        003
        004
    
    
        节点03
        005
        006
    

接下来Unity中写代码:

第一种方式

通过GetElementsByTagName直接获取节点,返回类型是XmlNodeList数组,数组包括了这个节点的所有内容

代码如何:

[csharp] view plain copy
print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Xml;  
  4.   
  5. public class DJH_Read : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.         string url = Application.dataPath + “/MyTest.xml”;  
  10.         XmlDocument XmlDoc=new XmlDocument();  
  11.         XmlDoc.Load(url);  
  12.   
  13.         int XmlCount = XmlDoc.GetElementsByTagName(“JD”)[0].ChildNodes.Count;  
  14.   
  15.         for (int i = 0; i < XmlCount; i++)  
  16.         {  
  17.             string NameValue = XmlDoc.GetElementsByTagName(“JD”)[0].ChildNodes[i].InnerText;  
  18.             Debug.Log(NameValue);  
  19.         }  
  20.   
  21.     }  
  22.   
  23. }  
using UnityEngine;
using System.Collections;
using System.Xml;

public class DJH_Read : MonoBehaviour {

    // Use this for initialization
    void Start () {
        string url = Application.dataPath + "/MyTest.xml";
        XmlDocument XmlDoc=new XmlDocument();
        XmlDoc.Load(url);

        int XmlCount = XmlDoc.GetElementsByTagName("JD")[0].ChildNodes.Count;

        for (int i = 0; i < XmlCount; i++)
        {
            string NameValue = XmlDoc.GetElementsByTagName("JD")[0].ChildNodes[i].InnerText;
            Debug.Log(NameValue);
        }

    }

}

输出后结果:

XML读取的几种方式_第1张图片


第二种方式

通过foreach查找所有目标名称的子节点

代码如下:

[csharp] view plain copy
print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Xml;  
  4.   
  5. public class DJH_Read : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.         string url = Application.dataPath + “/MyTest.xml”;  
  10.         XmlDocument XmlDoc=new XmlDocument();  
  11.         XmlDoc.Load(url);  
  12.   
  13.         XmlNodeList nodeList = XmlDoc.SelectSingleNode(”Xml”).ChildNodes;  
  14.         foreach (XmlElement xe in nodeList)  
  15.         {  
  16.             foreach (XmlElement xxe in xe.ChildNodes)  
  17.             {  
  18.                 if (xxe.Name == “Name”)  
  19.                 {  
  20.                     Debug.Log(xxe.InnerText);  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26. }  
using UnityEngine;
using System.Collections;
using System.Xml;

public class DJH_Read : MonoBehaviour {

    // Use this for initialization
    void Start () {
        string url = Application.dataPath + "/MyTest.xml";
        XmlDocument XmlDoc=new XmlDocument();
        XmlDoc.Load(url);

        XmlNodeList nodeList = XmlDoc.SelectSingleNode("Xml").ChildNodes;
        foreach (XmlElement xe in nodeList)
        {
            foreach (XmlElement xxe in xe.ChildNodes)
            {
                if (xxe.Name == "Name")
                {
                    Debug.Log(xxe.InnerText);
                }
            }
        }
    }

}

输出后结果:

XML读取的几种方式_第2张图片

你可能感兴趣的:(文件配置)