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

你可能感兴趣的文章
bcolz的新操作
查看>>
Linux的s、t、i、a权限(转)
查看>>
zmq的send
查看>>
C++中的delete加深认识
查看>>
windows消息机制(转)
查看>>
STL笔试面试题总结(干货)(转)
查看>>
XML 和 HTML 之间的差异
查看>>
qt中moc的作用
查看>>
阿里钉钉面试题
查看>>
华为社招笔试
查看>>
C++中找资源或者函数的方法
查看>>
一些留给自己的思考题(只求回过头来能够有所获)
查看>>
SQL函数返回表的写法
查看>>
delete对象时会自动调用类的析构函数
查看>>
C++ 子类对象直接赋值给父类对象可行,反过来不行
查看>>
linux下同一个动态库名为何辣么多的.so文件
查看>>
SQL联表的方式(逗号, Left Join, Right Join)
查看>>
牛客网输入输出举例
查看>>
字符串初始化时的注意点
查看>>
软考相关试题
查看>>