XML读取的几种方式

编写一个XML文件如下:

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

接下来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);
        }

    }

}

输出后结果:



第二种方式

通过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);
                }
            }
        }
    }

}

输出后结果:


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值