博客
关于我
简单的xml读取存储方法(未优化)
阅读量:389 次
发布时间: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/

你可能感兴趣的文章
Python 列表(List)概述-ChatGPT4o作答
查看>>
Python网络爬虫基础进阶到实战教程
查看>>
Python 初学者需要知道的四条建议
查看>>
Python 判断字符串是否包含子字符串
查看>>
python 判断当前时间是否为零点
查看>>
python 利用pandas读取本地中CSV文件的指定列 列名重命名 并保存回本地
查看>>
python 利用pyspark读取HDFS中CSV文件的指定列 列名重命名 并保存回HDFS
查看>>
python 利用pyttsx3文字转语音
查看>>
python 利用已有Ner模型进行数据清洗合并
查看>>
python 到大数据开发工程师_如何成为一个大数据开发工程师?
查看>>
python 加密解密(base64, AES)
查看>>
Python 包管理器和 Node.js
查看>>
python 启动提示IDLE's subprocess didn't make conne...
查看>>
Python 和 OpenCV.如何检测图像中的所有(填充)圆形/圆形对象?
查看>>
Python 和 RabbitMQ - 聆听来自多个渠道的消费事件的最佳方式?
查看>>
python编辑器打不开_关于命令ride.py打不开RF,而是打开pycharm编辑器问题解决思路...
查看>>
python编译exe同时支持32位_第三周:同时管理64位和32位版本的Python,并用Pyinstaller打包成exe...
查看>>
Python 和Java 哪个更适合做自动化测试?
查看>>
Python编程:掌握高级语言程序设计。从零基础到精通,收藏这篇就够了!
查看>>
python 图片转ico
查看>>