
简单的xml读取存储方法(未优化)
发布日期:2021-05-08 16:29:40
浏览次数:10
分类:精选文章
本文共 5274 字,大约阅读时间需要 17 分钟。
文章目录
使用Xml读取存储数据
0.原xml数据
shanJia 10 5 20 20 1 1 1 2 3 1 - 2
1.读取
代码如下(示例):
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{ 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"; Debug.Log(Application.persistentDataPath); 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 = this.name; root.AppendChild(name); XmlElement atk = xml.CreateElement("atk"); atk.InnerText = this.atk.ToString(); root.AppendChild(atk); XmlElement def = xml.CreateElement("def"); def.InnerText = this.def.ToString(); root.AppendChild(def); XmlElement moveSpeed = xml.CreateElement("moveSpeed"); moveSpeed.InnerText = this.moveSpeed.ToString(); root.AppendChild(moveSpeed); XmlElement roundSpeed = xml.CreateElement("roundSpeed"); roundSpeed.InnerText = this.roundSpeed.ToString(); root.AppendChild(roundSpeed); XmlElement weapon = xml.CreateElement("weapon"); XmlElement id = xml.CreateElement("id"); id.InnerText = this.weapon.id.ToString(); XmlElement num = xml.CreateElement("num"); num.InnerText = this.weapon.num.ToString(); weapon.AppendChild(id); weapon.AppendChild(num); root.AppendChild(weapon); XmlElement listInt = xml.CreateElement("listInt"); for (int i = 0; i < this.listInt.Count; i++) { XmlElement intNode = xml.CreateElement("int"); intNode.InnerText = this.listInt[i].ToString(); listInt.AppendChild(intNode); } root.AppendChild(listInt); XmlElement itemList = xml.CreateElement("itemList"); for (int i = 0; i < this.itemList.Count; i++) { XmlElement itemNode = xml.CreateElement("Item"); itemNode.SetAttribute("id", this.itemList[i].id.ToString()); itemNode.SetAttribute("num", this.itemList[i].num.ToString()); itemList.AppendChild(itemNode); } root.AppendChild(itemList); XmlElement itemDic = xml.CreateElement("itemDic"); foreach (int key in this.itemDic.Keys) { XmlElement intNode = xml.CreateElement("int"); intNode.InnerText = key.ToString(); itemDic.AppendChild(intNode); XmlElement itemNode = xml.CreateElement("Item"); itemNode.SetAttribute("id", this.itemDic[key].id.ToString()); itemNode.SetAttribute("num", this.itemDic[key].num.ToString()); itemDic.AppendChild(itemNode); } root.AppendChild(itemDic); //存储数据 xml.Save(path); }
总结
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年03月20日 17时21分42秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
omnet++
2021-05-08
23种设计模式一:单例模式
2021-05-08
Qt中的析构函数
2021-05-08
C语言实现dijkstra(adjacence matrix)
2021-05-08
三层框架+sql server数据库 实战教学-徐新帅-专题视频课程
2021-05-08
【单片机开发】智能小车工程(经验总结)
2021-05-08
【单片机开发】基于stm32的掌上游戏机设计 (项目规划)
2021-05-08
C++&&STL
2021-05-08
子集(LeetCode 78)
2021-05-08
微信js-sdk使用简述(分享,扫码功能等)
2021-05-08
mxsrvs支持thinkphp3.2伪静态
2021-05-08
c++中ifstream及ofstream超详细说明
2021-05-08
vuex modules
2021-05-08
sleep、wait、yield、join——简介
2021-05-08
web项目配置
2021-05-08
基于单片机简易信号误差分析设计-全套资料
2021-05-08
基于单片机简易脉搏测量仪系统设计-毕设课设资料
2021-05-08
Javascript中String支持使用正则表达式的四种方法
2021-05-08
eclipse引用sun.misc开头的类
2021-05-08
Servlet2.5的增删改查功能分析与实现------删除功能(四)
2021-05-08