首页 > 代码库 > u3d读取xml txt

u3d读取xml txt

u3d读取xml文件和u3d 读取txt外部文件

using UnityEngine;
using System.Collections;

using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;


public class u3dxml : MonoBehaviour
{
    private string m_filename = "2.txt";
    private string m_path =null ;


    // Use this for initialization
    void Start ()
    {
        XmlDocument xmlDoc = new XmlDocument();
        string xmlPath = Application.dataPath + "\\1.xml";
        
        //FileStream fs = new FileStream(xmlPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        xmlDoc.Load(xmlPath);
        //xmlDoc.Load(fs);//可以Load文件的路径也可以Load文件的流

        XmlNodeList componentList = xmlDoc.SelectSingleNode("ttx").ChildNodes;//获取car节点的所有子节点

        foreach (XmlNode componentXN in componentList)//遍历所有子节点,得到component结点
        {
            XmlElement componentXE = (XmlElement)componentXN;//将子节点类型转换为XmlElement类型


            XmlNodeList nls = componentXE.ChildNodes;//继续获取component子节点的所有子节点

            Debug.Log("data:" + nls.Item(0).InnerText);
            

           // dictionaryOfActionName.Add(int.Parse(nls.Item(0).InnerText), nls.Item(1).InnerText);//取值

        }


        readTXT();
    }
    
    // Update is called once per frame
    void Update ()
    {
    
    }
///////////////////////////////////////////////////////////////////////////// 1.txt
    void readTXT()
    {
        StreamReader m_reader =null;
        m_reader = File.OpenText(Application.dataPath + "\\" + m_filename);
        string s_line;

        while ((s_line = m_reader.ReadLine()) != null)
        {
           // Debug.Log(s_line);
            int numt;
            string[] words =s_line.Split(‘:‘);
            numt = int.Parse(words[1]);
            Debug.Log("num:" + numt);

        }
        m_reader.Close();
        m_reader.Dispose();
    }
}