标题 ##下面是代码
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
public class AddressData : MonoBehaviour
{
string filepath;
private void Start()
{
filepath = Application.dataPath + @"/Resources/my.xml";
createXml();
UpdateXml();
AddXml();
showXml();
}
public void createXml()
{
if (!File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("transforms");
XmlElement elmNew = xmlDoc.CreateElement("rotation");
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");
elmNew.AppendChild(rotation_X);
elmNew.AppendChild(rotation_Y);
elmNew.AppendChild(rotation_Z);
root.AppendChild(elmNew);
xmlDoc.AppendChild(root);
xmlDoc.Save(filepath);
Debug.Log("createXml OK!");
}
}
public void UpdateXml()
{
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") == "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!");
}
}
public void AddXml()
{
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!");
}
}
public void deleteXml()
{
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!");
}
}
public void showXml()
{
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);
}
}
}