【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.ChildNodes0.InnerText;

int age = int.Parse(heroElement.ChildNodes1.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/hero2/name 获取第二个hero节点

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

//heros/heroposition() \< 3/name 获取前两个节点

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

*/

XmlNodeList list = xmlDocument.SelectNodes("//heros/heroposition() \< 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 = namesi;

heroElement.AppendChild(nameElement);

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

ageElement.InnerText = agesi.ToString();

heroElement.AppendChild(ageElement);

herosElement.AppendChild(heroElement);

}

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

}

相关推荐
anlog1 小时前
ini文件读取,EXE同目录文件读取
c#·ini
rockey6272 小时前
AScript之编译递归函数
c#·.net·script·动态脚本
yue0088 小时前
winform控件篇-按钮
c#
DevOpenClub11 小时前
PDF 多格式解析如何避免混用输出:TEXT、HTML、XML 与 TAG 数据契约
xml·前端·数据库·pdf·html
微三云生态系统架构师-彭丹11 小时前
任务卷轴积分系统架构设计:从任务状态机到合规风控的完整实现
unity·系统架构·游戏引擎
DevOpenClub12 小时前
Markdown、HTML 和 PPT 如何稳定交付:文档转换任务的幂等发布流程
开发语言·前端·c#·html·powerpoint
XR技术研习社13 小时前
从 PICO 文档看未来短期内的 Unity 版本选择
unity·游戏引擎·xr·vr
软件黑马王子13 小时前
3.单例模式问题1:构造函数
开发语言·单例模式·c#
软件黑马王子14 小时前
4.单例模式问题2:重复挂载
开发语言·单例模式·c#
夏霞15 小时前
c# 不支持的目标框架 解决方案
开发语言·c#