xml 本地数据存储功能
这个是为了记录一些xml 保存数据的功能
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
public class DisplayXMLManager
{
public static Dictionary<string, int> RankData = new Dictionary<string, int>();
static string filepath = Application.streamingAssetsPath + @"/display.xml";
public static void CreateXml(string Name,string VScore)
{
if (!File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("transforms");
XmlElement elmNew = xmlDoc.CreateElement(Name);
XmlElement rotation_X = xmlDoc.CreateElement("score");
rotation_X.InnerText = VScore;
elmNew.AppendChild(rotation_X);
root.AppendChild(elmNew);
xmlDoc.AppendChild(root);
xmlDoc.Save(filepath);
Debug.Log("createXml OK!");
}
}
public static void AddXml(string Name, string XValue)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode root = xmlDoc.SelectSingleNode("transforms");
XmlElement elmNew = xmlDoc.CreateElement(Name);
XmlElement rotation_X = xmlDoc.CreateElement("score");
rotation_X.InnerText = XValue;
elmNew.AppendChild(rotation_X);
root.AppendChild(elmNew);
xmlDoc.AppendChild(root);
xmlDoc.Save(filepath);
Debug.Log("AddXml OK!");
}
else
{
CreateXml(Name,XValue);
}
}
public static void UpdateXml(string name,string score)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach (XmlElement xe in nodeList)
{
if (xe.Name == name)
{
foreach (XmlElement x1 in xe.ChildNodes)
{
if (x1.Name == "score")
{
x1.InnerText = score;
}
}
break;
}
}
xmlDoc.Save(filepath);
Debug.Log("UpdateXml OK!");
}
}
public static bool HaveThisType(string type)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach (XmlElement xe in nodeList)
{
if (xe.Name == type)
{
return true;
}
}
}
return false;
}
/// <summary>
/// 删除配置文件
/// </summary>
public static void DeleteXmlFile()
{
File.Delete(filepath);
}
public static void LoadXml()
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach (XmlElement xe in nodeList)
{
foreach (XmlElement x1 in xe.ChildNodes)
{
if (x1.Name == "score")
{
RankData.Add(xe.Name, Convert.ToInt32(x1.InnerText));
}
}
}
}
}
public static int RandkCount()
{
int RandkCount = 0;
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach (XmlElement xe in nodeList)
{
RandkCount++;
}
}
return RandkCount;
}
public static int MinScore()
{
int number = 0;
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach (XmlElement xe in nodeList)
{
foreach (XmlElement x1 in xe.ChildNodes)
{
if (x1.Name == "score")
{
if (number >= Convert.ToInt32(x1.InnerText))
{
number = Convert.ToInt32(x1.InnerText);
}
}
}
}
}
Debug.Log("minscore:"+number);
return number;
}
public static float ReadValue(string type)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode node = xmlDoc.SelectSingleNode("//transforms/" + type);
if (node != null)
{
return StrToFloat(node.InnerText);
}
else
{
return 0.0f;
}
}
public static float StrToFloat(object FloatString)
{
float result;
if (FloatString != null)
{
if (float.TryParse(FloatString.ToString(), out result))
return result;
else
{
return (float)0.00;
}
}
else
{
return (float)0.00;
}
}
}