【Unity】使用XML进行数据读存的简单例子

//解析Xml方法一

//被读取文件

<?xml version = "1.0" encoding="utf-8"?>

<root>

<heros>

<hero id = "1">

<name>超人</name>

<age>35</age>

</hero>

<hero>

<name>蝙蝠侠</name>

<age>37</age>

</hero>

<hero id = "3">

<name>闪电侠</name>

<age>20</age>

</hero>

</heros>

</root>

void ParseXML1()

{

//读取文件

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.Load(Application.dataPath + "/XML/test.xml");

XmlElement rootElement = xmlDocument.LastChild as XmlElement;//获取root节点

XmlElement herosElement = rootElement.FirstChild as XmlElement;//获取heros节点

foreach(XmlElement heroElement in herosElement)

{

string id = heroElement.GetAttribute("id");//获取id属性

string name = heroElement.ChildNodes[0].InnerText;

int age = int.Parse(heroElement.ChildNodes[1].InnerText);

Debug.Log(id + " " + name + " " + age);//1 超人 35 蝙蝠侠 37 3 闪电侠 20

}

}

//解析Xml方法二(XPath)

void ParseXML2()

{

XmlDocument xmlDocument = new XmlDocument();//读取文件

xmlDocument.Load(Application.dataPath + "/XML/test.xml");

/*

* XPath语法:

/root/heros/hero/name 访问name节点

//name 相对路径

//heros/hero[2]/name 获取第二个hero节点

//heros/hero[last() - 1] 获取倒数第二个hero节点

//heros/hero[position() < 3]/name 获取前两个节点

//heros/hero[@id = 3]/name 获取hero节点中id=3的节点下的name节点

*/

XmlNodeList list = xmlDocument.SelectNodes("//heros/hero[position() < 3]");//获取所有符合要求的节点的列表

foreach(XmlElement element in list)

{

Debug.Log(element.InnerText);//超人35 蝙蝠侠37

}

}

//创建XML

/*

<?xml version="1.0" encoding="utf-8"?>

<root>

<heros>

<hero id1="0" id2="0">

<name>超人</name>

<age>20</age>

</hero>

<hero id1="1" id2="1">

<name>蝙蝠侠</name>

<age>27</age>

</hero>

<hero id1="2" id2="2">

<name>闪电侠</name>

<age>23</age>

</hero>

</heros>

</root>

*/

void CreateXML()

{

XmlDocument xmlDocument = new XmlDocument();//创建Xml文件

XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", "");//创建文档声明

xmlDocument.AppendChild(xmlDeclaration);

XmlElement rootElement = xmlDocument.CreateElement("root");//创建root节点

xmlDocument.AppendChild(rootElement);

XmlElement herosElement = xmlDocument.CreateElement("heros");

rootElement.AppendChild(herosElement);

//循环数据

string[] names = new string[] { "超人", "蝙蝠侠", "闪电侠" };

int[] ages = new int[] { 20, 27, 23 };

for(int i = 0; i < 3; i ++)

{

XmlElement heroElement = xmlDocument.CreateElement("hero");//创建hero节点

heroElement.SetAttribute("id1", i + "");//设置属性值方法一

//设置属性值方法二

XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("id2");//设置节点属性值

xmlAttribute.Value = i + "";//设置属性值

heroElement.Attributes.Append(xmlAttribute);

XmlElement nameElement = xmlDocument.CreateElement("name");//创建name节点

nameElement.InnerText = names[i];

heroElement.AppendChild(nameElement);

XmlElement ageElement = xmlDocument.CreateElement("age");//创建age节点

ageElement.InnerText = ages[i].ToString();

heroElement.AppendChild(ageElement);

herosElement.AppendChild(heroElement);

}

xmlDocument.Save(Application.dataPath + "/XML/test2.xml");//保存文件

}

相关推荐
死也不注释6 小时前
【Unity 编辑器工具开发:GUILayout 与 EditorGUILayout 对比分析】
unity·编辑器·游戏引擎
葬歌倾城8 小时前
JSON的缩进格式方式和紧凑格式方式
c#·json
Eiceblue10 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
小小小小王王王10 小时前
hello判断
开发语言·c#
金增辉12 小时前
基于C#的OPCServer应用开发,引用WtOPCSvr.dll
c#
future141214 小时前
C#学习日记
开发语言·学习·c#
傻啦嘿哟15 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
小赖同学啊15 小时前
物联网中的Unity/Unreal引擎集成:数字孪生与可视化控制
物联网·unity·游戏引擎
Zlzxzw19 小时前
使用unity创建项目,进行动画制作
unity·游戏引擎
唐青枫20 小时前
C#.NET log4net 详解
c#·.net