XML文件的读取,更改,填写

添加脚本LoadXMLConfiguration.cs,如图绑定到摄像机对象上。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;
using UnityEngine.UI;


public class LoadXMLConfiguration : MonoBehaviour
{
    private ArrayList DialogA = new ArrayList();
    private ArrayList DialogA = new ArrayList();
    void Awake()
    {
	LoadXML();
    }
	
    //配置读取上图中的XMLDate.xml文件
    void LoadXML()
    {
	//创建XML文档
	XmlDocument xmlDocument = new XmlDocument();
	//创建Xml文件的配置
	XmlReaderSetting xmlSet = new XmlRraderSetting();
	//这个设置是为了忽略xml注释文档的影响,有时候注释会影响到xml文件的读取。
	xmlSet.IgnoreComments = true;
	
	//加载XMLData.xml文件
	xmlDocument.Load(XmlReader.Create((Application.dataPath + "/XMLFile/XMLDate.xml"),xmlSet));
	
	//获取Objects节点下的所有子节点
	XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("Objects").ChildNodes;
	foreach(XmlElement element in xmlNodeList)
	{
	    if(element.GetAttribute("id") == "1")
	    {
		foreach(XmlElement element1 in element.ChildNodes)
		{
		    if(element1.GetAttribute("Monitor") == "true")
		    {
			print("element1.GetAttribute("Name") + ":" + element1.GetAttribute("Content"));
		    }
		}
	    }
	}
    }
    //将内容上传到XML配置文件中
    void UpDateXML()
    {
	string path = Application.dataPath + "/XMLFile/XMLDate.xml";
	
	if(File.Exists(paht))
	{
	    XmlDocument xmlDocument = new XmlDocument();
	    xmlDocument.Load(path);
	    XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("Objects").ChildNodes;
	    
	    foreach(XmlElement element in xmlNodeList)
	    {
		if(element.GetAttribute("id") == "1")
		{
		    //把id = 1的子项更改为: id = 5;
		    element.SetAttribute("id","5");
		}
		if(element.GetAttribute("id") == "2")
		{
		    foreach(XmlElement element1 in element.ChildNodes)
		    {
			if(element1.GetAttribute("Name") == "1")
			{
			    element1.SetAttribute("Name","123");
			    element1.InnerText = "我改变了世界。";
			}
  		    }
		}
	    }
	    xmlDocument.Save(path);
	}
    }
    //创建XML文件 XMLDate1.xml
 
 
    void CreateXMLFile()
    {
	string path = Application.dataPath + "/XMLFile/XMLDate1.xml";
	if(!File.Exists(path))
	{
	    //创建最上一层节点
	    XmlDocument xmlDocument = new XmlDocuemnt();
	    //创建最上一层节点
	    XmlElement root = xmlDocument.CreaeteElement("Objects");
	    //创建子节点
	    XmlElement xmlElement = xmlDocument.CreateElement("Messages");
	    xmlElement.SetAtttibute("name","操作流程");
	    XmlElement xmlElement_1 = xmlDocument.CreateElement("Action");
 	    xmlElement_1.SetAttribute("id","1");
	    xmlElement_1.SetAttribute("Monitor","false");
//设置节点中的内容
 
 
	    xmlElement_1.InnerText = "打开PLC进行操作。";
 
 
	    XmlElement xmlElement_2 = xmlDocument.CreateElement("Action");
 	    xmlElement_2.SetAttribute("id","2");
	    xmlElement_2.SetAttribute("Monitor","true");
//设置节点中的内容
 
 
	    xmlElement_2.InnerText = "打开阀门,消毒水流出。";
 
 
 
 
	    //把节点一层一层的加入到XML文件中,注意他们之间的先后顺序,这是生成XML文件的顺序。
	    xmlElement.AppendChild(xmlElement_1);
            xmlElement.AppendChild(xmlElement_2);
root.AppendChild(xmlElement);
 
 
	    xmlDocument.AppendChild(root);
	    xmlDocument.Save(path);
	}
    }

    //向XML文件中添加内容
    void AddXMLData()
    {
	//文件路径
	string path = Application.dataPath + "/XMLFile/XMLDate2.xml";

	if(!File.Exists(path))
	{
	    XmlDocument xmlDocument = new XmlDocument();
	    
	    //根目录
    	    XmlElement root = xmlDocument.CreateElement("Objects");

     	    //根目录下的第一个  子目录
	    XmlElement xmlElement = xmlDocument.CreateElement("Messages");
	    xmlElement.SetAttribute("Name","游戏执行顺序。");

	    //根目录下的第二个  子目录
	    XmlElement xmlElement_1 = xmlDocument.CreateElement("Message");
	    xmlElement_1.SetAttribute("Id","1");
	    xmlElement_1.SetAttribute("Action","进入大门。");
	    xmlElement_1.SetAttribute("Monitor","true");
	    xmlElement_1.InnerText = "任务执行开始。";

	    xmlElement.AppendChild(xmlElement_1);
	    root.AppendChild(xmlElement);
	    xmlDocument.AppendChild(root);
	    
	    xmlDocument.Save(path);
	}
	else
	{
	    XmlDocument xmlDocument = new XmlDocument();
	    xmlDocument.Load(path);

	    //找到Objects节点
	    XmlNode root = xmlDocument.SelectSingleNode("Objects");
	   
	    //找到Messages节点
 	    XmlNode xmlElement = root.SelectSingleNode("Messages");

 	    //新建节点
	    XmlElement xmlElement = xmlDocument.CreateElement("Message");
	    xmlElement_1.SetAttribute("Id", "2");
            xmlElement_1.SetAttribute("Action", "与门卫对话");
            xmlElement_1.SetAttribute("Monitor", "true");
            xmlElement_1.InnerText = "你好,欢迎来到火影忍者。";


            XmlElement xmlElement_2 = xmlDocument.CreateElement("Message");
            xmlElement_2.SetAttribute("Id","3");
            xmlElement_2.SetAttribute("Action","你好,请接受任务。");
            xmlElement_2.SetAttribute("Monitor", "false");
            xmlElement_2.InnerText = "升级";


            xmlElement.AppendChild(xmlElement_1);
            xmlElement.AppendChild(xmlElement_2);


            root.AppendChild(xmlElement);
            xmlDocument.AppendChild(root);


            xmlDocument.Save(path);
	}
    }
 
 
}



 
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自blog.csdn.net/MonoBehaviour/article/details/73927473