讀取五種格式的配置檔案(xml(兩種方式),txt,excel,csv,json)
阿新 • • 發佈:2019-02-05
using Mono.Xml;
using System.Security;
using LitJson;
using System.Collections.Generic;
using System.IO;
using Excel;
using System.Data;
//六種讀取檔案的方式
public class ReadInfo : MonoBehaviour {
// Use this for initialization void Start () { //讀取TXT檔案 //ReadTXT(); //用system.xml讀取xml檔案 //ReadXML(); //用mono.xml讀取xml檔案 //ReadXML1(); //讀取json檔案 //ReadJson(); //讀取CSV檔案 //ReadCSV(); //讀取excel檔案 ReadExcel(); } //讀取excel檔案 private static void ReadExcel() { //路徑拼接找到excel檔案的路徑 string path = Application.dataPath + @"\Excel\Test.xlsx"; Debug.Log(path); //以位元組流的形式載入讀取 FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read); //轉型為IExcelDataReader型別 IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(stream); //轉化為DataSet格式 DataSet result = reader.AsDataSet(); //獲取列數和行數 int columns = result.Tables[0].Columns.Count; int rows = result.Tables[0].Rows.Count; //遍歷每一行 for(int i = 1; i < rows; i ++) { Enemy enemy = new Enemy(); //讀取第一張表的第i行的第幾個配置資訊 enemy.name = result.Tables[0].Rows[i][0].ToString(); enemy.hp = int.Parse(result.Tables[0].Rows[i][1].ToString()); enemy.attackPoint = int.Parse( result.Tables[0].Rows[i][2].ToString()); enemy.ToString(); } } //讀取CSV檔案 private static void ReadCSV() { List<Enemy> list = new List<Enemy>(); //載入csv檔案 TextAsset csv = Resources.Load("CSV/Test") as TextAsset; //檔案切割為每一行 string[] lines = csv.text.Split('\n'); Debug.Log(lines.Length); //遍歷每一行的資料 每一行的資料是以逗號隔開的 for (int i = 1; i < lines.Length; i++) { string[] temp = lines[i].Split(','); Debug.Log(temp.Length); Enemy enemy = new Enemy(); enemy.name = temp[0]; enemy.hp = double.Parse(temp[1]); enemy.attackPoint = double.Parse(temp[2]); enemy.ToString(); list.Add(enemy); } } //讀取Json檔案 private static void ReadJson() { //Debug.Log( data["family_list"][1]["name"]); List<Famliy> list = new List<Famliy>(); //載入json檔案 TextAsset json = Resources.Load("JSON/Test") as TextAsset; //轉化為JsonData型別的資料 JsonData data = JsonMapper.ToObject(json.text); //進行遍歷 family_list相當對根節點 for (int i = 0; i < data["family_list"].Count; i++) { Famliy temp = new Famliy(); //根節點下的第幾組資料 中的哪一個屬性 temp.name = data["family_list"][i]["name"].ToString(); temp.age = int.Parse(data["family_list"][i]["age"].ToString()); temp.tellphone = data["family_list"][i]["tellphone"].ToString(); temp.address = data["family_list"][i]["address"].ToString(); temp.ToString(); list.Add(temp); } } //mono.xml讀取xml檔案 private static void ReadXML1() { //載入xml檔案 TextAsset xml = Resources.Load("XML/Test") as TextAsset; //建立一個安全的語法剖析程式 SecurityParser sp = new SecurityParser(); //讀取xml資訊 sp.LoadXml(xml.text); //轉化為安全元素型別 SecurityElement se = sp.ToXml(); //遍歷這個根元素裡的子元素 foreach (SecurityElement child in se.Children) { //如果子元素的標籤為enemy,獲取子元素的屬性 if (child.Tag == "Enemy") { Debug.Log(child.Attribute("name")); } //巢狀的元素 if (child.Tag == "root1") { //遍歷該元素下的所有元素 foreach (SecurityElement child1 in child.Children) { Debug.Log(child1.Attribute("name")); } } } } //system.xml讀取xml檔案 private static void ReadXML() { //載入xml檔案 TextAsset xml = Resources.Load("XML/Test") as TextAsset; //宣告一個XmlDocument的物件 XmlDocument document = new XmlDocument(); //xml的檔案資訊載入到上一步宣告的物件中 document.LoadXml(xml.text); //找到root的根節點 XmlNode root = document.SelectSingleNode("root"); //獲取此節點下的所有節點 XmlNodeList nodeList = root.ChildNodes; //遍歷所有節點 foreach (XmlNode node in nodeList) { //節點的巢狀 if (node.Name == "root1") { foreach (XmlNode node1 in node.ChildNodes) { Debug.Log(node1.Name); //將節點轉化為xml元素 進行屬性的獲取 XmlElement element1 = node1 as XmlElement; Debug.Log(element1.GetAttribute("name")); } continue; } Debug.Log(node.Name); XmlElement element = node as XmlElement; Debug.Log(element.GetAttribute("name")); } } //讀取txt檔案 private static void ReadTXT() { //載入txt檔案 TextAsset txt = Resources.Load("TXT/Test") as TextAsset; //Debug.Log(txt.text); //字串的切割 獲取資訊 string[] str = txt.text.Split('\n'); for (int i = 0; i < str.Length; i++) { Debug.Log("字元陣列的長度: " + str.Length); Debug.Log(str[i]); } }
}
public class Famliy
{
public string name;
public int age;
public string tellphone;
public string address;
public new void ToString()
{
Debug.Log(name + age + tellphone + address);
}
}
public class Enemy
{
public string name;
public double hp;
public double attackPoint;
public new void ToString()
{
Debug.Log(name + hp + attackPoint );
}
}