Xml的增删改查

来源:互联网 发布:php在线支付源码 编辑:程序博客网 时间:2024/05/22 19:44
间:2017/4/21
 * 
 * 编写:    **
 * 
 * 介绍:    属于静态类,用于写入和读取XML文件.(当出现修改不成功时代表目标节点不存在)
 * 
 * 功能模块:1.创建XML文件;
 *           2.给目标XML文件添加节点;
 *           3.给目标XML文件删除节点;
 *           4.修改目标XML文件节点的值;
 *           5.获取目标XML文件中某节点的值;
 * 
 * 公司:    **
 */


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


public static class WriteAndReadXML
{


    public static void CreateXML(string path)
    {
        if (File.Exists(path))
            return;


        int count = 0;
        for (int i = path.Length - 1; i > 0; --i)
        {
            if (path[i] != '/')
                ++count;
            else if (path[i] == '/')
                break;
        }
        StringBuilder _path = new StringBuilder(path.Length);
        for (int i = 0; i < path.Length - count - 1; ++i)
        {
            _path.Append(path[i]);
        }
        //Debug.Log(_path);


        if (!Directory.Exists(_path.ToString()))
            Directory.CreateDirectory(_path.ToString());


        XmlDocument doc = new XmlDocument();
        XmlElement root = doc.CreateElement("Root");
        doc.AppendChild(root);
        doc.Save(path);
    }


    public static string GetNodeValue(string path, string nodeName, string ElementName)
    {
        if (!File.Exists(path))
        {
            Debug.Log("获取节点的值失败,该路径下的文件不存在!");
            return "0";
        }


        string value;


        XmlDocument doc = new XmlDocument();
        doc.Load(path);


        XmlNodeList nodeList = doc.SelectSingleNode("Root").ChildNodes;
        for (int i = 0; i < nodeList.Count; ++i)
        {
            XmlElement element = (XmlElement)nodeList[i];
            if (element.Name == nodeName && element.GetAttributeNode(ElementName) != null)
            {
                value = element.GetAttribute(ElementName);
                return value;
            }
        }


        return "该节点不存在";
    }


    public static void AddNode(string path, string nodeName, string ElementName, string ElementValue)
    {
        if (!File.Exists(path))
        {
            Debug.Log("该路径下的文件不存在!请先创建该文件.");
            return;
        }


        XmlDocument doc = new XmlDocument();
        doc.Load(path);


        XmlNode root = doc.SelectSingleNode("Root");
        XmlNodeList nodeList = root.ChildNodes;
        for (int i = 0; i < nodeList.Count; ++i)
        {
            XmlElement element = (XmlElement)nodeList[i];


            if (element.Name == nodeName)
                return;
        }
        XmlElement node = doc.CreateElement(nodeName);
        node.SetAttribute(ElementName, ElementValue);


        root.AppendChild(node);
        doc.Save(path);
    }


    public static void AddAttribute(string path, string nodeName, string attributeName, string value)
    {
        if (!File.Exists(path))
        {
            Debug.Log("添加属性失败,该路径下的文件不存在!");
            return;
        }


        XmlDocument doc = new XmlDocument();
        doc.Load(path);


        XmlNodeList nodeList = doc.SelectSingleNode("Root").ChildNodes;
        for (int i = 0; i < nodeList.Count; ++i)
        {
            XmlElement element = (XmlElement)nodeList[i];
            if (element.Name == nodeName)
            {
                if (element.GetAttributeNode(attributeName) == null)
                {
                    XmlAttribute attribute = doc.CreateAttribute(attributeName);
                    attribute.Value = value;
                    element.SetAttributeNode(attribute);
                    break;
                }
            }
        }


        doc.Save(path);
    }


    public static void ChangeNodeValue(string path, string nodeName, string ElementName, string ChangeValue)
    {
        if (!File.Exists(path))
        {
            Debug.Log("修改节点的值失败,该路径下的文件不存在!");
            return;
        }


        XmlDocument doc = new XmlDocument();
        doc.Load(path);


        XmlNodeList nodeList = doc.SelectSingleNode("Root").ChildNodes;
        for (int i = 0; i < nodeList.Count; ++i)
        {
            XmlElement element = (XmlElement)nodeList[i];
            if (element.Name == nodeName && element.GetAttributeNode(ElementName) != null)
            {
                element.SetAttribute(ElementName, ChangeValue);
            }
        }


        doc.Save(path);
    }


    public static void DelectNode(string path, string NodeName)
    {
        if (!File.Exists(path))
        {
            Debug.Log("删除节点失败,该路径下的文件不存在!");
            return;
        }


        XmlDocument doc = new XmlDocument();
        doc.Load(path);


        XmlNode root = doc.SelectSingleNode("Root");
        XmlNodeList nodeList = root.ChildNodes;
        for (int i = 0; i < nodeList.Count; ++i)
        {
            XmlElement element = (XmlElement)nodeList[i];
            if (element.Name == NodeName)
            {
                root.RemoveChild(element);
                if (i < nodeList.Count) i = i - 1;
            }
        }


        doc.Save(path);
    }
}
原创粉丝点击