关闭

XML的增删改查

标签: c#unityxmlstringIO
231人阅读 评论(0) 收藏 举报
分类:

关于XML的增删改查的研究 下面的是测试代码

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

public class XMLScript : MonoBehaviour {

	
	void Start () {
       // CreateXml();
        //ShowXml();
       // AddXml();
        //ChangeXml();
        DeleteXml();
	}
	
	void Update () {
	
	}
    //创建XML
    void CreateXml() {    
        string filePath = Application.dataPath + "/Xml/Lc.xml";
        if (!File.Exists(filePath))
        {
            XmlDocument xmldoc = new XmlDocument();//实例化一个XML文件
            //创建更节点
            XmlElement root = xmldoc.CreateElement("threeHello");
            //创建子节点:版本
            XmlNode version = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //创建子节点:注释
            XmlNode commentNode = xmldoc.CreateComment("LCs threeHello_2015.10.16");

            //
            XmlElement lc1 = xmldoc.CreateElement("Lc1sthreeHello");
            lc1.SetAttribute("id", "0");
            lc1.SetAttribute("name", "long");

            XmlElement x_Hello = xmldoc.CreateElement("x");
            x_Hello.InnerText = "90";
            XmlElement y_Hello = xmldoc.CreateElement("y");
            y_Hello.InnerText = "17";
            XmlElement z_Hello = xmldoc.CreateElement("z");
            z_Hello.InnerText = "105";
            //将xyz_hello放进到lc1
            lc1.AppendChild(x_Hello);
            lc1.AppendChild(y_Hello);
            lc1.AppendChild(z_Hello);
            //lc1放入根节点的root
            root.AppendChild(lc1);

            //将版本,根节点,注释
            xmldoc.AppendChild(version);
            xmldoc.AppendChild(root);
            xmldoc.AppendChild(commentNode);
            xmldoc.Save(filePath);
        }
        
    }
    //查看
    void ShowXml()
    {
        string filePath = Application.dataPath + "/Xml/Lc.xml";
        if (File.Exists(filePath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);//查看
            //通过找到更节点,找到其中的子节点
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("threeHello").ChildNodes;//找一个节点
            foreach (XmlElement item in nodeList)
            {
                Debug.Log(item.GetAttribute("id"));
                Debug.Log(item.GetAttribute("name"));
                foreach (XmlElement item2 in item.ChildNodes)
                {
                    Debug.Log(item2.Name);
                    Debug.Log(item2.InnerText);
                }
            }
        }
    }
    //增加
    void AddXml() {
        string filePath = Application.dataPath + "/Xml/Lc.xml";
        if (File.Exists(filePath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);
            XmlElement lc2 = xmlDoc.CreateElement("Lc2sthreeHello");
            lc2.SetAttribute("id", "1");
            lc2.SetAttribute("name", "liu");

            XmlElement x_Hello = xmlDoc.CreateElement("x");
            x_Hello.InnerText = "83";
            XmlElement y_Hello = xmlDoc.CreateElement("y");
            y_Hello.InnerText = "16.5";
            XmlElement z_Hello = xmlDoc.CreateElement("z");
            z_Hello.InnerText = "97";
            lc2.AppendChild(x_Hello);
            lc2.AppendChild(y_Hello);
            lc2.AppendChild(z_Hello);

            XmlNode root = xmlDoc.SelectSingleNode("threeHello");
            root.AppendChild(lc2);
            xmlDoc.Save(filePath);
        }
    }
    void ChangeXml()
    {
        string filePath = Application.dataPath + "/Xml/Lc.xml";
        if (File.Exists(filePath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);//查看
            //通过找到更节点,找到其中的子节点
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("threeHello").ChildNodes;//找一个节点
            foreach (XmlElement item in nodeList)
            {
                Debug.Log(item.GetAttribute("id"));
                Debug.Log(item.GetAttribute("name"));
                if (item.Name=="Lc1sthreeHello")
                {
                    foreach (XmlElement item2 in item.ChildNodes)
                    {
                        Debug.Log(item2.Name);
                        Debug.Log(item2.InnerText);
                        if (item2.Name == "y")
                        {
                            item2.InnerText = "0.5";
                        }
                    } 
                }      
            }
            xmlDoc.Save(filePath);
        }    
    }

      void DeleteXml() {
          string filePath = Application.dataPath + "/Xml/Lc.xml";
          if (File.Exists(filePath))
          {
              XmlDocument xmlDoc = new XmlDocument();
              xmlDoc.Load(filePath);//查看
              //通过找到更节点,找到其中的子节点
              XmlNodeList nodeList = xmlDoc.SelectSingleNode("threeHello").ChildNodes;//找一个节点
              foreach (XmlElement item in nodeList)
              {
                  Debug.Log(item.GetAttribute("id"));
                  Debug.Log(item.GetAttribute("name"));
                  item.RemoveAttribute("id");
                  if (item.Name == "Lc1sthreeHello")
                  {
                      foreach (XmlElement item2 in item.ChildNodes)
                      {
                          Debug.Log(item2.Name);
                          Debug.Log(item2.InnerText);
                         // item1.RemoveChild(item2.FirstChild);
                          if (item2.Name == "y")
                          {
                              item2.InnerText = "0.5";
                              item2.RemoveAll();//只是删除数字
                             
                          }
                      }
                  }
              }
              xmlDoc.Save(filePath);
          }    
      }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43915次
    • 积分:824
    • 等级:
    • 排名:千里之外
    • 原创:37篇
    • 转载:8篇
    • 译文:1篇
    • 评论:2条
    文章分类
    最新评论