博客
关于我
简单的xml读取存储方法(未优化)
阅读量:388 次
发布时间:2019-03-05

本文共 5144 字,大约阅读时间需要 17 分钟。

使用XML读取存储数据

0. 原XML数据

以下是一个示例的原始XML数据结构,用于存储玩家信息:

shanJia
10
5
20
20
1
1
1
2
3
1
2
3

1. 读取数据

以下是读取XML数据的示例代码:

using System.Collections;using System.Collections.Generic;using System.IO;using System.Xml;using UnityEngine;public class Item{    public int id;    public int num;}public class PlayerInfo : MonoBehaviour{    public string name;    public int atk;    public int def;    public float moveSpeed;    public float roundSpeed;    public Item weapon = new Item();    public List
listInt = new List
(); public List
itemList = new List
(); public Dictionary
itemDic = new Dictionary
(); public void LoadData(string fileName) { string path = Application.persistentDataPath + "/" + fileName + ".xml"; if (!File.Exists(path)) { path = Application.streamingAssetsPath + "/" + fileName + ".xml"; } XmlDocument xml = new XmlDocument(); xml.Load(path); XmlNode root = xml.SelectSingleNode("PlayerInfo"); name = root.SelectSingleNode("name").InnerText; atk = int.Parse(root.SelectSingleNode("atk").InnerText); def = int.Parse(root.SelectSingleNode("def").InnerText); moveSpeed = int.Parse(root.SelectSingleNode("moveSpeed").InnerText); roundSpeed = int.Parse(root.SelectSingleNode("roundSpeed").InnerText); weapon.id = int.Parse(root.SelectSingleNode("weapon").SelectSingleNode("id").InnerText); weapon.num = int.Parse(root.SelectSingleNode("weapon").SelectSingleNode("num").InnerText); XmlNode intList = root.SelectSingleNode("listInt"); XmlNodeList lsit = intList.SelectNodes("int"); for (int i = 0; i < lsit.Count; i++) { listInt.Add(int.Parse(lsit[i].InnerText)); } XmlNodeList Itemlist = root.SelectSingleNode("itemList").SelectNodes("Item"); for (int i = 0; i < Itemlist.Count; i++) { Item t = new Item(); t.id = int.Parse(Itemlist[i].Attributes["id"].Value); t.num = int.Parse(Itemlist[i].Attributes["num"].Value); itemList.Add(t); } XmlNodeList dic = root.SelectSingleNode("itemDic").SelectNodes("Item"); foreach (XmlNode item in dic) { Item item1 = new Item(); item1.id = int.Parse(item.Attributes["id"].Value); item1.num = int.Parse(item.Attributes["num"].Value); itemDic.Add(item1.id, item1); } }}

2. 存储数据

以下是存储数据的示例代码:

public void SaveData(string fileName){    string path = Application.persistentDataPath + "/" + fileName + ".xml";    XmlDocument xml = new XmlDocument();    XmlDeclaration xmlDec = xml.CreateXmlDeclaration("1.0", "UTF-8", "");    xml.AppendChild(xmlDec);    XmlElement root = xml.CreateElement("PlayerInfo");    xml.AppendChild(root);    XmlElement name = xml.CreateElement("name");    name.InnerText = name;    root.AppendChild(name);    XmlElement atk = xml.CreateElement("atk");    atk.InnerText = atk.ToString();    root.AppendChild(atk);    XmlElement def = xml.CreateElement("def");    def.InnerText = def.ToString();    root.AppendChild(def);    XmlElement moveSpeed = xml.CreateElement("moveSpeed");    moveSpeed.InnerText = moveSpeed.ToString();    root.AppendChild(moveSpeed);    XmlElement roundSpeed = xml.CreateElement("roundSpeed");    roundSpeed.InnerText = roundSpeed.ToString();    root.AppendChild(roundSpeed);    XmlElement weapon = xml.CreateElement("weapon");    XmlElement id = xml.CreateElement("id");    id.InnerText = weapon.id.ToString();    XmlElement num = xml.CreateElement("num");    num.InnerText = weapon.num.ToString();    weapon.AppendChild(id);    weapon.AppendChild(num);    root.AppendChild(weapon);    XmlElement listInt = xml.CreateElement("listInt");    for (int i = 0; i < listInt.Count; i++)    {        XmlElement intNode = xml.CreateElement("int");        intNode.InnerText = listInt[i].ToString();        listInt.AppendChild(intNode);    }    root.AppendChild(listInt);    XmlElement itemList = xml.CreateElement("itemList");    for (int i = 0; i < itemList.Count; i++)    {        XmlElement itemNode = xml.CreateElement("Item");        itemNode.SetAttribute("id", itemList[i].id.ToString());        itemNode.SetAttribute("num", itemList[i].num.ToString());        itemList.AppendChild(itemNode);    }    root.AppendChild(itemList);    XmlElement itemDic = xml.CreateElement("itemDic");    foreach (int key in itemDic.Keys)    {        XmlElement intNode = xml.CreateElement("int");        intNode.InnerText = key.ToString();        itemDic.AppendChild(intNode);        XmlElement itemNode = xml.CreateElement("Item");        itemNode.SetAttribute("id", itemDic[key].id.ToString());        itemNode.SetAttribute("num", itemDic[key].num.ToString());        itemDic.AppendChild(itemNode);    }    root.AppendChild(itemDic);    xml.Save(path);}

总结

通过以上代码示例,可以实现对XML数据的读取和存储。读取部分主要是从XML文件中提取数据并赋值给相应的变量,存储部分则是将游戏中的数据以XML格式写入文件中。这种方式可以方便地进行数据的交互和管理。

转载地址:http://fvwzz.baihongyu.com/

你可能感兴趣的文章
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>