简单的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);    }

总结

上一篇:Xml数据管理类(优化存储读取数据)结合反射知识点
下一篇:Unity使用GUI做屏幕自适应(完)

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年03月20日 17时21分42秒