在Unity引擎中如何生成本地XML数据?
第一步:引用C#的命名空间System.Xml
第二步:生成XML文档(XmlDocument类)
第三步:生成根元素(XmlElement类)添加给文档对象
第四步:循环生成子元素添加给父元素
第五步:将⽣成的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";
//判断当前路径下是否已经存在该文件
if(!File.Exists(filePath)){
//1.生成XML文档实例
XmlDocument xmlDoc=new XmlDocument( );
//生成声明语句
XmlNode dec=xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
xmlDoc.AppendChild(dec);
//2.生成根节点
XmlElement root=xmlDoc.CreateElement("Cube");
//3.生成子节点
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");
//设置属性
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");
//设置属性
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);
//获取将要添加的节点的父节点(只能从父节点一层一层往下找)
XmlNode trans=xmlDoc.SelectSingleNode("Cube").SelectSingleNode("Transform");
//创建新的子节点
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){
//获取标签,判断是否是要删除的标签
if(xmlEle.Name=="GameObject"){
if(xmlEle.GetAttribute("name")=="Cube"){
//删除属性
//xmlEle.RemoveAttribute("name");
//删除节点
//xmlEle.RemoveAll();
//更改属性
xmlEle.SetAttribute("layer","Nothing");
//更改节点
//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;
}
}