using UnityEngine;
using System.Collections;
using System.Xml;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System;
public class TestXml : MonoBehaviour
{
Skill skill = new Skill ();
string address = "ldy.txt";
void Start ()
{
CreateXml ();
}
//创建一个XML文档
public void CreateXml ()
{
//生成文档
XmlDocument xmlDoc = new XmlDocument ();
//创建根元素
XmlElement root = xmlDoc.CreateElement ("skills");
//添加到父元素上
xmlDoc.AppendChild (root);
//穿件ID元素
XmlElement id = xmlDoc.CreateElement ("id");
//设置内容
id.InnerText = 2.ToString ();
//添加到节点
root.AppendChild (id);
XmlElement name = xmlDoc.CreateElement ("name");
name.InnerText = "李东阳";
root.AppendChild (name);
//保存内容
xmlDoc.Save (Application.dataPath + "//" + "MyInfo.xml");
//刷新
AssetDatabase.Refresh ();
}
//载入一个XML文档
public void LoadXml ()
{
//初始化一个doc
XmlDocument xmlDoc = new XmlDocument ();
//加载路径(方法1) 后面直接跟路径
xmlDoc.Load (Application.dataPath + "//" + "MyInfo.xml");
//方法二 将XML转化为 一个string 加载
//xmlDoc.LoadXml (File.ReadAllText("AssetsMyInfo"));
//获取第一个节点
XmlNode root = xmlDoc.FirstChild;
//获取当前节点下的所有子节点
XmlNodeList list = root.ChildNodes;
XmlNode listNode;
for (int i = 0; i < list.Count; i++) {
if (list [i].Name == "id") {
listNode = list.Item (i);
skill.Id = Convert.ToInt32 (listNode.InnerText);
File.AppendAllText (Application.dataPath + "//" + address, Convert.ToString (skill.Id));
} else if (list [i].Name == "name") {
listNode = list.Item (i);
skill.Name = listNode.InnerText;
File.AppendAllText (Application.dataPath + "//" + address, skill.Name);
}
}
}
void OnGUI ()
{
if (GUILayout.Button ("AAAA")) {
LoadXml ();
AssetDatabase.Refresh ();
}
}
}
public class Skill
{
public int Id{ get; set; }
public string Name{ get; set; }
}
//其中读取xml 还有其他方式