Unity-XML文件创建、读取、删除、更新、添加
1、XML文件----创建
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
public class CreateXML :MonoBehaviour{
public string localPath; //创建路径
public static CreateXML instance; //单例
private void Awake()
{
instance=this;
}
//文件创建
public void Create_XML()
{
localPath = Application.streamingAssetsPath + "/" + "MyXML.xml";
if (!File.Exists(localPath))
{
XmlDocument xml = new XmlDocument();
XmlDeclaration xmldecl = xml.CreateXmlDeclaration("1.0", "UTF-8", "");//设置xml文件编码格式为UTF-8
XmlElement root = xml.CreateElement("Data");//创建根节点
XmlElement info = xml.CreateElement("Info");//创建子节点
info.SetAttribute("Name", "小王");//创建子节点属性名和属性值
info.SetAttribute("Age", "28");
info.SetAttribute("Phone", "12345678");
root.AppendChild(info);//将子节点按照创建顺序,添加到xml
xml.AppendChild(root);
xml.Save(localPath);//保存xml到路径位置
Debug.Log("创建XML成功!");
}
}
}
2、XML文件----添加
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
public class WriteINXML : MonoBehaviour {
public string localPath;
private void Start()
{
AddXML();
}
void AddXML()
{
localPath = Application.streamingAssetsPath + "/" + "MyXML.xml";
if (File.Exists(localPath))
{
XmlDocument xml = new XmlDocument();
xml.Load(localPath);//加载xml文件
XmlNode root = xml.SelectSingleNode("Data");//获取根节点
XmlElement info = xml.CreateElement("Info_2");//创建新的子节点
info.SetAttribute("Name", "小李");//创建新子节点属性名和属性值
info.SetAttribute("Age", "25");
info.SetAttribute("Phone", "87654321");
root.AppendChild(info);//将子节点按照创建顺序,添加到xml
xml.AppendChild(root);
xml.Save(localPath);//保存xml到路径位置
Debug.Log("添加XML成功!");
}
}
}
3、XML文件----读取
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
public class ReadXML : MonoBehaviour {
public string localPath;
void Start ()
{
Readxml();
}
void Readxml()
{
localPath = Application.streamingAssetsPath + "/" + "MyXML.xml";
if (File.Exists(localPath))
{
XmlDocument xml = new XmlDocument();
xml.Load(localPath);//加载xml文件
XmlNodeList nodeList = xml.SelectSingleNode("Data").ChildNodes;
foreach (XmlElement xe in nodeList)
{//遍历所有子节点
if (xe.Name == "Info_2")
{
Debug.Log(xe.GetAttribute("Name"));//获取Name属性值
Debug.Log(xe.GetAttribute("Age"));
Debug.Log(xe.GetAttribute("Phone"));
}
}
Debug.Log("读取XML成功!" + xml.OuterXml);
}
}
}
4、XML文件----更新
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
public class UpdateXML : MonoBehaviour {
public string localPath;
void Start () {
UpdateXml();
}
public void UpdateXml()
{
localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml";
if (File.Exists(localPath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(localPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Data").ChildNodes;
foreach (XmlElement xe in nodeList)
{
//拿到节点中属性Name =小王的节点
if (xe.GetAttribute("Name") == "小王")
{
//更新节点属性
xe.SetAttribute("Age", "21");
break;
}
}
xmlDoc.Save(localPath);
Debug.Log("更新XML成功!");
}
}
}
5、XML文件----删除
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
public class DeleteXML : MonoBehaviour {
public string localPath;
void Start () {
DeleteXml();
}
public void DeleteXml()
{
localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml";
if (File.Exists(localPath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(localPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Data").ChildNodes;
foreach (XmlElement xe in nodeList)
{
if (xe.GetAttribute("Name") == "小李")
{
xe.RemoveAttribute("Phone");
}
if (xe.GetAttribute("Name") == "小王")
{
xe.RemoveAll();
}
}
xmlDoc.Save(localPath);
Debug.Log("删除XML成功!");
}
}
}