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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值