xml 本地数据存储功能

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;
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值