在Unity引擎中如何生成本地XML數(shù)據(jù)?
第一步:引用C#的命名空間System.Xml
第二步:生成XML文檔(XmlDocument類(lèi))
第三步:生成根元素(XmlElement類(lèi))添加給文檔對(duì)象
第四步:循環(huán)生成子元素添加給父元素
第五步:將?成的XML文檔保存
usingUnityEngine;
usingSystem.Collections;
usingSystem.Xml;
usingSystem.IO;
usingSystem.Collections.Generic;
publicclassXMLScript:MonoBehaviour{
voidStart( ){
//CreatXml( );
//AddXml( );
//DeleXml( );
//showXml( );
Dictionarydic=showXml( );
foreach(stringkeyindic.Keys){
print(key+":"+dic[key]);
}
}
//生成XML
public void CreatXml( ){
//獲得Xml保存路徑(放在Assets文件夾下)
string filePath=Application.dataPath+"/XmlFile/myXml.xml";
//判斷當(dāng)前路徑下是否已經(jīng)存在該文件
if(!File.Exists(filePath)){
//1.生成XML文檔實(shí)例
XmlDocument xmlDoc=new XmlDocument( );
//生成聲明語(yǔ)句
XmlNode? dec=xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
xmlDoc.AppendChild(dec);
//2.生成根節(jié)點(diǎn)
XmlElement root=xmlDoc.CreateElement("Cube");
//3.生成子節(jié)點(diǎn)
XmlElement? trans=xmlDoc.CreateElement("Transform");
XmlElement? pos=xmlDoc.CreateElement("position");
XmlElement? x=xmlDoc.CreateElement("x");
x.InnerText="10";
XmlElement? y=xmlDoc.CreateElement("y");
y.InnerText="20";
XmlElement? z=xmlDoc.CreateElement("z");
z.InnerText="30";
XmlElement? gameElement=xmlDoc.CreateElement("GameObject");
//設(shè)置屬性
gameElement.SetAttribute("name","Cube");
gameElement.SetAttribute("tag","Player");
XmlAttribute? att=xmlDoc.CreateAttribute("layer");
att.Value="Everything";
gameElement.SetAttributeNode(att);
XmlElement?? sta=xmlDoc.CreateElement("Static");
sta.InnerText="NavigationStatic";
gameElement.AppendChild(sta);
root.AppendChild(gameElement);
XmlElement? gameElement_1=xmlDoc.CreateElement("GameObject");
//設(shè)置屬性
gameElement_1.SetAttribute("name","Sphere");
gameElement_1.SetAttribute("tag","Player");
XmlAttribute att_1=xmlDoc.CreateAttribute("layer");
att_1.Value="Everything";
gameElement_1.SetAttributeNode(att_1);
XmlElement sta_1=xmlDoc.CreateElement("Static");
sta_1.InnerText="NavigationStatic";
gameElement_1.AppendChild(sta_1);
root.AppendChild(gameElement_1);
root.AppendChild(trans);
pos.AppendChild(x);
pos.AppendChild(y);
pos.AppendChild(z);
trans.AppendChild(pos);
xmlDoc.AppendChild(root);
//4.保存到本地
xmlDoc.Save(filePath);
}
}
public void AddXml( ){
string filePath=Application.dataPath+"/XmlFile/myXml.xml";
if(File.Exists(filePath)){
XmlDocument? xmlDoc=newXmlDocument( );
//加載已有的XML文件夾
xmlDoc.Load(filePath);
//獲取將要添加的節(jié)點(diǎn)的父節(jié)點(diǎn)(只能從父節(jié)點(diǎn)一層一層往下找)
XmlNode? trans=xmlDoc.SelectSingleNode("Cube").SelectSingleNode("Transform");
//創(chuàng)建新的子節(jié)點(diǎn)
XmlElement? newElement=xmlDoc.CreateElement("rotation");
XmlElement? x=xmlDoc.CreateElement("x");
x.InnerText="10";
XmlElement? y=xmlDoc.CreateElement("y");
y.InnerText="20";
XmlElement? z=xmlDoc.CreateElement("z");
z.InnerText="30";
newElement.AppendChild(x);
newElement.AppendChild(y);
newElement.AppendChild(z);
trans.AppendChild(newElement);
xmlDoc.Save(filePath);
}
}
public void? DeleXml( ){
string? filePath=Application.dataPath+"/XmlFile/myXml.xml";
if(File.Exists(filePath)){
XmlDocument? xmlDoc=newXmlDocument( );
xmlDoc.Load(filePath);
XmlNode? root=xmlDoc.SelectSingleNode("Cube");
XmlNodeList?? nodeList=root.ChildNodes;
foreach(XmlElement?? xmlEle? in? nodeList){
//獲取標(biāo)簽烤宙,判斷是否是要?jiǎng)h除的標(biāo)簽
if(xmlEle.Name=="GameObject"){
if(xmlEle.GetAttribute("name")=="Cube"){
//刪除屬性
//xmlEle.RemoveAttribute("name");
//刪除節(jié)點(diǎn)
//xmlEle.RemoveAll();
//更改屬性
xmlEle.SetAttribute("layer","Nothing");
//更改節(jié)點(diǎn)
//xmlEle.InnerText="";
}
}
}
xmlDoc.Save(filePath);
}
}
public? Dictionary? showXml( ){
string? filePath=Application.dataPath+"/XmlFile/myXml.xml";
Dictionary<string,string>? dic=new? Dictionary<string,string>( );
if(File.Exists(filePath)){
XmlDocumentxml Doc=new XmlDocument( );
xmlDoc.Load(filePath);
XmlNodeList? nodeList=xmlDoc.SelectSingleNode("Cube").ChildNodes;
foreach(XmlElementxmlEleinnodeList){
if(xmlEle.Name=="Transform"){
XmlNodeList? childList=xmlEle.SelectSingleNode("position").ChildNodes;
foreach(XmlElementxeinchildList){
//存入字典
dic.Add(xe.Name,xe.InnerText);
stringstr="true";
print(bool.Parse(str));
}
}
}
}
return? dic;
}
}