Unity上使用Linq To XML——簡單易維護的代碼

項目裏經常會碰到XML的使用,操作用法無非就是增刪改查。使用C#的原始XML來寫非常麻煩,但是使用Linq to XML來寫就相對簡潔的多了。首先,創建一個腳本,名爲Linq to XML,去掉它繼承與MonoBehaviour。代碼如下:

本帖隱藏的內容

[C#] 純文本查看 複製代碼
using unityEngine;
using System.Collections;
using System.Linq;
using System.Xml.Linq;
using System;

public class XML {
//static string xmlpath = Application.persistentDataPath + @"\myXML";//平臺相關的路徑(移動端)
static string xmlpath=Application.dataPath+@"\mydfdfXML";//電腦上的路徑,移動端沒有這個訪問權限
/// <summary>
/// 初始化一個XML文件
/// </summary>
public static void CreateXMLDocument()
{
XElement root = new XElement("XMLContent",
new XElement("Herb1",new XAttribute("MyVaule","0")),
new XElement("Herb2",new XAttribute("MyVaule","0")),
new XElement("Herb3",new XAttribute("MyVaule","0")),
new XElement("Pill1",new XAttribute("MyVaule","0")),
new XElement("Pill2",new XAttribute("MyVaule","0")),
new XElement("Pill3",new XAttribute("MyVaule","0")),
new XElement("Level",new XAttribute("MyVaule","0")),
new XElement("Root","root")
);
root.Save(xmlpath);
}
public static XElement LoadXMLFromFile()
{
XElement root = XElement.Load(xmlpath);
return root;
}
public static void SetElementValue(string name, string value)
{
XElement root = LoadXMLFromFile();
root.Element(name).SetAttributeValue("MyVaule", value);
root.Save(xmlpath);
}
/// <summary>
/// 在根節點元素之前添加新的元素
/// </summary>
/// <param name="name">元素名字</param>
/// <param name="value">元素的值</param>
public static void AddElement(string name, string value)
{
XElement root = LoadXMLFromFile();
root.Element("Root").AddBeforeSelf(new XElement(name, new XAttribute("MyValue",value)));
root.Save(xmlpath);
}
/// <summary>
/// 刪除指定的元素
/// </summary>
/// <param name="name">要刪除的元素名稱</param>
public static void RemoveElement(string name)
{
XElement root = LoadXMLFromFile();
root.Element(name).Remove();
root.Save(xmlpath);
}
/// <summary>
/// 根據元素名查找元素對應的值
/// </summary>
/// <param name="name">元素名</param>
/// <returns></returns>
public static string GetElementValue(string name)
{
XElement root = LoadXMLFromFile();
XAttribute xattr = root.Element(name).Attribute("MyVaule");
string s = xattr.Value;
return s;
}
}
項目裏經常會碰到XML的使用,操作用法無非就是增刪改查。使用C#的原始XML來寫非常麻煩,但是使用Linq to XML來寫就相對簡潔的多了。首先,創建一個腳本,名爲Linq to XML,去掉它繼承與MonoBehaviour。代碼如下:

本帖隱藏的內容

[C#] 純文本查看 複製代碼
using unityEngine;
using System.Collections;
using System.Linq;
using System.Xml.Linq;
using System;

public class XML {
//static string xmlpath = Application.persistentDataPath + @"\myXML";//平臺相關的路徑(移動端)
static string xmlpath=Application.dataPath+@"\mydfdfXML";//電腦上的路徑,移動端沒有這個訪問權限
/// <summary>
/// 初始化一個XML文件
/// </summary>
public static void CreateXMLDocument()
{
XElement root = new XElement("XMLContent",
new XElement("Herb1",new XAttribute("MyVaule","0")),
new XElement("Herb2",new XAttribute("MyVaule","0")),
new XElement("Herb3",new XAttribute("MyVaule","0")),
new XElement("Pill1",new XAttribute("MyVaule","0")),
new XElement("Pill2",new XAttribute("MyVaule","0")),
new XElement("Pill3",new XAttribute("MyVaule","0")),
new XElement("Level",new XAttribute("MyVaule","0")),
new XElement("Root","root")
);
root.Save(xmlpath);
}
public static XElement LoadXMLFromFile()
{
XElement root = XElement.Load(xmlpath);
return root;
}
public static void SetElementValue(string name, string value)
{
XElement root = LoadXMLFromFile();
root.Element(name).SetAttributeValue("MyVaule", value);
root.Save(xmlpath);
}
/// <summary>
/// 在根節點元素之前添加新的元素
/// </summary>
/// <param name="name">元素名字</param>
/// <param name="value">元素的值</param>
public static void AddElement(string name, string value)
{
XElement root = LoadXMLFromFile();
root.Element("Root").AddBeforeSelf(new XElement(name, new XAttribute("MyValue",value)));
root.Save(xmlpath);
}
/// <summary>
/// 刪除指定的元素
/// </summary>
/// <param name="name">要刪除的元素名稱</param>
public static void RemoveElement(string name)
{
XElement root = LoadXMLFromFile();
root.Element(name).Remove();
root.Save(xmlpath);
}
/// <summary>
/// 根據元素名查找元素對應的值
/// </summary>
/// <param name="name">元素名</param>
/// <returns></returns>
public static string GetElementValue(string name)
{
XElement root = LoadXMLFromFile();
XAttribute xattr = root.Element(name).Attribute("MyVaule");
string s = xattr.Value;
return s;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章