/*************************************************************************
* FileName: XMLTest.cs
* Author: LaiZhangYin(Eagle) Version: 1.0 Date: 6/24/2017
* if you have some question, please call
* QQ/Wechat : 782966734
*************************************************************************/
using System.IO;
using UnityEngine;
using System.Collections;
using System.Xml;
using UnityEditor;
public class XMLTest : MonoBehaviour
{
private string path = Application.dataPath + @"\XMLData/xmlData.xml";
private ArrayList Adialogue = new ArrayList();
private ArrayList Bdialogue = new ArrayList();
private ArrayList textList = new ArrayList();
[ContextMenu("CreateXML")]
public void CreateXML()
{
// string path = Application.dataPath + @"\XMLData/xmlData.xml";
if (!File.Exists(path))
{
XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("objects");
XmlElement element = xml.CreateElement("messages");
element.SetAttribute("id", "1");
XmlElement elementChild1 = xml.CreateElement("contents");
elementChild1.SetAttribute("name", "a");
elementChild1.InnerText = "It's you,Eagle!!!";
XmlElement elementChild2 = xml.CreateElement("mission");
elementChild2.SetAttribute("map", "abc");
elementChild2.InnerText = "go, man ! To realize your dream!";
element.AppendChild(elementChild1);
element.AppendChild(elementChild2);
root.AppendChild(element);
xml.AppendChild(root);
xml.Save(path);
}
AssetDatabase.Refresh();
}
[ContextMenu("AddXMLData")]
public void AddXMLData()
{
// string path = Application.dataPath + @"\XMLData/xmlData.xml";
if (File.Exists(path))
{
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNode root = xml.SelectSingleNode("objects");
XmlElement element = xml.CreateElement("messages");
element.SetAttribute("id", "1");
XmlElement elementChild1 = xml.CreateElement("contents");
elementChild1.SetAttribute("name", "a");
elementChild1.InnerText = "It's you,Eagle!!!";
XmlElement elementChild2 = xml.CreateElement("mission");
elementChild2.SetAttribute("map", "abc");
elementChild2.InnerText = "go, man ! To realize your dream!";
element.AppendChild(elementChild1);
element.AppendChild(elementChild2);
root.AppendChild(element);
xml.AppendChild(root);
xml.Save(path);
}
AssetDatabase.Refresh();
}
[ContextMenu("ReadXML")]
public void LoadXML()
{
XmlDocument xml = new XmlDocument();
XmlReaderSettings set = new XmlReaderSettings();
set.IgnoreComments = true;
xml.Load(XmlReader.Create(path, set));
XmlNodeList xmlNodeList = xml.SelectSingleNode("objects").ChildNodes;
foreach (XmlElement xl1 in xmlNodeList)
{
if(xl1.GetAttribute("id") == "1")
{
foreach (XmlElement xl2 in xl1.ChildNodes)
{
textList.Add(xl2.GetAttribute("name") + " : " + xl2.InnerText);
if(xl2.GetAttribute("name") == "a")
{
Adialogue.Add(xl2.GetAttribute("name") + " : " + xl2.InnerText);
}
if(xl2.GetAttribute("map") == "abc")
{
Adialogue.Add(xl2.GetAttribute("map") + " : " + xl2.InnerText);
}
}
}
}
for (int i = 0; i < Adialogue.Count; i++)
{
print(Adialogue[i]);
}
}
[ContextMenu("UpdateXML")]
public void UpdateXMLData()
{
if(File.Exists(path))
{
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNodeList xmlNodeList = xml.SelectSingleNode("objects").ChildNodes;
foreach (XmlElement xl1 in xmlNodeList)
{
if(xl1.GetAttribute("id") == "5")
{
xl1.SetAttribute("id", "5");
foreach (XmlElement xl2 in xl1.ChildNodes)
{
xl2.SetAttribute("map", "df");
xl2.InnerText = "I have change you successfully";
}
}
}
xml.Save(path);
}
AssetDatabase.Refresh();
print("OK");
}
}