找回密码
 立即注册
查看: 440|回复: 0

[Unity2012北京开发者大会] nity3d XML的创建、更新、添加、删除和读取

[复制链接]
发表于 2021-6-7 09:12 | 显示全部楼层 |阅读模式

关于Xml文件的相关操作

创建、更新、添加、删除、读取

using System.IO;

using System.Xml;

using UnityEngine;


public class XmlOperation : MonoBehaviour

{

    private string path = "";


    void Awake()

    {

        path= Application.dataPath + "/test.xml";

    }


    void Start()

    {

        //CreateXml(path);

        //UpdateXml(path,"0");

        //AddXml(path);

        //DeleteXml(path,"1001");

        ReadXml(path);

    }


    /// <summary>

    /// 根据路径来创建xml文件

    /// </summary>

    /// <param name="filePath"></param>

    public void CreateXml(string filePath)

    {

        if (!File.Exists(filePath))

        {

            // 创建xml文档实例

            XmlDocument xmlDoc = new XmlDocument();

            // 创建根节点

            XmlElement root = xmlDoc.CreateElement("transforms");

            // 创建第一个子节点

            XmlElement elmXml = xmlDoc.CreateElement("rotation");

            // 设置节点属性

            elmXml.SetAttribute("id", "0");

            elmXml.SetAttribute("name", "first");

            // 创建第一子节点的子节点

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


            // 排序

            elmXml.AppendChild(rotation_x);

            elmXml.AppendChild(rotation_y);

            elmXml.AppendChild(rotation_z);

            root.AppendChild(elmXml);

            xmlDoc.AppendChild(root);


            xmlDoc.Save(filePath);

        }

    }


    /// <summary>

    /// 更新指定id的xml数据

    /// </summary>

    /// <param name="path"></param>

    /// <param name="id"></param>

    public void UpdateXml(string path, string id)

    {

        if (File.Exists(path))

        {

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(path);


            XmlNodeList nodes = xmlDoc.SelectSingleNode("transforms").ChildNodes;


            foreach (XmlElement xe in nodes)

            {

                if (xe.GetAttribute("id") == id)

                {

                    xe.SetAttribute("id", "1001");


                    foreach (XmlElement xx1 in xe.ChildNodes)

                    {

                        if (xx1.Name == "x")

                            xx1.InnerText = "1001";

                    }

                    break;

                }

            }


            xmlDoc.Save(path);

        }

    }

    /// <summary>

    /// 添加一条数据

    /// </summary>

    /// <param name="path"></param>

    public void AddXml(string path)

    {

        if (File.Exists(path))

        {

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(path);

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

            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(path);

        }

    }

    /// <summary>

    /// 删除数据

    /// </summary>

    /// <param name="path"></param>

    /// <param name="id"></param>

    public void DeleteXml(string path, string id)

    {

        if (File.Exists(path))

        {

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(path);

            XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;

            foreach (XmlElement xe in nodeList)

            {

                if (xe.GetAttribute("id") == id)

                {

                    // 移除指定id的属性

                    xe.RemoveAttribute("id");

                }


                foreach (XmlElement x1 in xe.ChildNodes)

                {

                    // 移除所有z的value

                    if (x1.Name == "z")

                    {

                        x1.RemoveAll();


                    }

                }

            }

            xmlDoc.Save(path);

        }

    }


    /// <summary>

    /// 读取xml

    /// </summary>

    /// <param name="path"></param>

    public void ReadXml(string path)

    {

        if (File.Exists(path))

        {

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(path);

            XmlNodeList nodes = xmlDoc.SelectSingleNode("transforms").ChildNodes;

            foreach (XmlElement xe in nodes)

            {

                Debug.Log("ID: " + xe.GetAttribute("id"));

                Debug.Log("Name: "+xe.GetAttribute("name"));


                string element = "";

                foreach (XmlElement x in xe)

                {

                    element= element + "," + x.InnerText;

                }

                Debug.Log(element);

            }

        }

    }

}



懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-1 21:40 , Processed in 0.117718 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表