Unity 읽 기 XML 에 대한 간단 한 학습

5812 단어 xmlC#unity3d
본문 은 선우 송 의 블 로그 에서 옮 겨 졌 다.  http://www.xuanyusong.com/archives/1901
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;

public class MyXML : MonoBehaviour {

    public void CreatXml()
    {
        //  XML   ,    
        string filePath = Application.dataPath + @"/";
        //             
        if (!File.Exists(filePath))
        {
            //  XML    
            XmlDocument xmlDoc = new XmlDocument();
            //  root  ,        
            XmlElement root = xmlDoc.CreateElement("transforms");
            //         
            XmlElement elmNew = xmlDoc.CreateElement("rotation");
            //          ID NAME
            elmNew.SetAttribute("id", "0");
            elmNew.SetAttribute("name", "eagle");

            //         
            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");

            //          xmlDoc ,         ,      XML     
            elmNew.AppendChild(rotation_X);
            elmNew.AppendChild(rotation_Y);
            elmNew.AppendChild(rotation_Z);
            root.AppendChild(elmNew);
            xmlDoc.AppendChild (root);
            // XML        
            xmlDoc.Save(filePath);
            Debug.Log("    ");
        }
    }

    public void UpdateXml()
    {
        string filePath = Application.dataPath + @"/";
        if (File.Exists(filePath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            //     XML     
            xmlDoc.Load(filePath);
            //  transform      
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("transform").ChildNodes;
            //       
            foreach (XmlElement  xe in nodeList )
            {
                //       ID=0   
                if (xe.GetAttribute("id") == "0")
                {
                    //      
                    xe.SetAttribute("id", "1000");
                    //    
                    foreach (XmlElement  xl in xe.ChildNodes )
                    {

                        if (xl.Name == "z")
                            xl.InnerText = "update0000"; //              ,             。
                    }
                    break;
                }
            }
            xmlDoc.Save(filePath);
            Debug.Log("    ");
        }
    }

    public void AddXml()
    {
        string filepath = Application.dataPath + @"/";
        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", "   ");

            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("    ");
        }
    }

    public void DeleteXml()
    {
        string filePath = Application.dataPath + @"/";
        if (File.Exists(filePath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("transform").ChildNodes;
            foreach (XmlElement  xe in nodeList )
            {
                if (xe.GetAttribute("id") == "1")
                {
                    xe.RemoveAttribute("id");
                }
                foreach (XmlElement  xl in xe.ChildNodes )
                {
                    if (xl.Name == "z")
                        xl.RemoveAll();
                }
            }
            xmlDoc.Save(filePath);
            Debug.Log("    ");
        }
    }

    public void ShowXml()
    {
        string filePath = Application.dataPath + @"/";
        if (File.Exists(filePath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("transform").ChildNodes;
            //       ,             
            foreach (XmlElement  xe in nodeList )
            {
                Debug.Log("Attribute" + xe.GetAttribute("name"));
                Debug.Log("NAME" + xe.Name);
                foreach (XmlElement  xl in xe.ChildNodes )
                {
                    if (xl.Name == "y")
                        Debug.Log("VALUE:" + xl.InnerText);
                }
            }
            Debug.Log("ALL=" + xmlDoc.OuterXml);
        }

    }
}

좋은 웹페이지 즐겨찾기