首先注意頭文件:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
1、生成XML:
public void createXml()
{
//xml保存的路徑,這裏放在Assets路徑 注意路徑。
string filepath = Application.dataPath + @"/my.xml";
//繼續判斷當前路徑下是否有該文件
if(!File.Exists (filepath))
{
//創建XML文檔實例
XmlDocument xmlDoc = new XmlDocument();
//創建root節點,也就是最上一層節點
XmlElement root = xmlDoc.CreateElement("transforms");
//繼續創建下一層節點
XmlElement elmNew = xmlDoc.CreateElement("rotation");
//設置節點的兩個屬性 ID 和 NAME
elmNew.SetAttribute("id","0");
elmNew.SetAttribute("name","momo");
//繼續創建下一層節點
XmlElement rotation_X = xmlDoc.CreateElement("x");
//設置節點中的數值
rotation_X.InnerText = "0";
XmlElement rotation_Y = xmlDoc.CreateElement("y");
rotation_Y.InnerText = "1";
XmlElement rotation_Z = xmlDoc.CreateElement("z");
rotation_Z.InnerText = "2";
//這裏在添加一個節點屬性,用來區分。。
rotation_Z.SetAttribute("id","1");
//把節點一層一層的添加至XMLDoc中 ,請仔細看它們之間的先後順序,這將是生成XML文件的順序
elmNew.AppendChild(rotation_X);
elmNew.AppendChild(rotation_Y);
elmNew.AppendChild(rotation_Z);
root.AppendChild(elmNew);
xmlDoc.AppendChild(root);
//把XML文件保存至本地
xmlDoc.Save(filepath);
Debug.Log("createXml OK!");
}
}
運行結果:
<transforms>
<rotation id="0" name="momo">
<x>0</x>
<y>1</y>
<z id="1">2</z>
</rotation>
</transforms>
2.更新XML文件:
以其中某個節點名稱做條件,當查詢到時更新該節點
public void UpdateXml()
{
string filepath = Application.dataPath + @"/my.xml";
if(File.Exists (filepath))
{
XmlDocument xmlDoc = new XmlDocument();
//根據路徑將XML讀取出來
xmlDoc.Load(filepath);
//得到transforms下的所有子節點
XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;
//遍歷所有子節點
foreach(XmlElement xe in nodeList)
{
//拿到節點中屬性ID =0的節點
if(xe.GetAttribute("id")=="0")
{
//更新節點屬性
xe.SetAttribute("id","1000");
//繼續遍歷
foreach(XmlElement x1 in xe.ChildNodes)
{
if(x1.Name=="z")
{
//這裏是修改節點名稱對應的數值,而上面的拿到節點連帶的屬性。。。
x1.InnerText="update00000";
}
}
break;
}
}
xmlDoc.Save(filepath);
Debug.Log("UpdateXml OK!");
}
}
運行結果:
<transforms>
<rotation id="1000" name="momo">
<x>0</x>
<y>1</y>
<z id="1">update00000</z>
</rotation>
</transforms>
3.添加XML:
public void AddXml()
{
string filepath = Application.dataPath + @"/my.xml";
if(File.Exists (filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode root = xmlDoc.SelectSingleNode("transforms");
XmlElement elmNew = xmlDoc.CreateElement("rotation");
elmNew.SetAttribute("id","1");
elmNew.SetAttribute("name","yusong");
XmlElement rotation_X = xmlDoc.CreateElement("x");
rotation_X.InnerText = "0";
rotation_X.SetAttribute("id","1");
XmlElement rotation_Y = xmlDoc.CreateElement("y");
rotation_Y.InnerText = "1";
XmlElement rotation_Z = xmlDoc.CreateElement("z");
rotation_Z.InnerText = "2";
elmNew.AppendChild(rotation_X);
elmNew.AppendChild(rotation_Y);
elmNew.AppendChild(rotation_Z);
root.AppendChild(elmNew);
xmlDoc.AppendChild(root);
xmlDoc.Save(filepath);
Debug.Log("AddXml OK!");
}
}
運行結果:
<transforms>
<rotation id="1000" name="momo">
<x>0</x>
<y>1</y>
<z id="1">update00000</z>
</rotation>
<rotation id="1" name="yusong">
<x id="1">0</x>
<y>1</y>
<z>2</z>
</rotation>
</transforms>
4.刪除XML:
public void deleteXml()
{
string filepath = Application.dataPath + @"/my.xml";
if(File.Exists (filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach(XmlElement xe in nodeList)
{
if(xe.GetAttribute("id")=="1")
{
xe.RemoveAttribute("id");
}
foreach(XmlElement x1 in xe.ChildNodes)
{
if(x1.Name == "z")
{
x1.RemoveAll();
}
}
}
xmlDoc.Save(filepath);
Debug.Log("deleteXml OK!");
}
}
運行結果:
<transforms>
<rotation id="1000" name="momo">
<x>0</x>
<y>1</y>
<z>
</z>
</rotation>
<rotation name="yusong">
<x id="1">0</x>
<y>1</y>
<z>
</z>
</rotation>
</transforms>
5.解析與輸出上面的XML:
public void showXml()
{
string filepath = Application.dataPath + @"/my.xml";
if(File.Exists (filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;
//遍歷每一個節點,拿節點的屬性以及節點的內容
foreach(XmlElement xe in nodeList)
{
Debug.Log("Attribute :" + xe.GetAttribute("name"));
Debug.Log("NAME :" + xe.Name);
foreach(XmlElement x1 in xe.ChildNodes)
{
if(x1.Name == "y")
{
Debug.Log("VALUE :" + x1.InnerText);
}
}
}
Debug.Log("all = " + xmlDoc.OuterXml);
}
}
原文:http://blog.csdn.net/liulala16/article/details/8679024