c# XML Helper

来源:互联网 发布:怎么用ps做淘宝细节图 编辑:程序博客网 时间:2024/05/17 01:41
 

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

namespace Helper
{
    public class XmlHelper
    {
        private static XmlHelper _xmlHelper;
        /// <summary>
        /// 获取Helper的日子
        /// </summary>
        public string HelperLog
        {
            private set;
            get;
        }
        public static XmlHelper GetInstance()
        {
            if (_xmlHelper == null)
            {
                _xmlHelper = new XmlHelper();
            }
            return _xmlHelper;
        }

        XmlHelper()
        {

        }
        /// <summary>
        /// 创建一个Xml文档
        /// </summary>
        /// <param name="XmlFilePath">xml文档路径</param>
        /// <param name="RootNode">根节点名称,为空则默认“root”</param>
        /// <returns>创建是否成功</returns>
        public bool CreateXmlDocument(string XmlFilePath,string RootNode)
        {
            try
            {
                HelperLog = "";
                XmlDocument doc= new XmlDocument();
                if (String.IsNullOrEmpty(RootNode))
                {
                    RootNode = "root";
                }
                doc.LoadXml("<"+RootNode+"/>");
                //XmlElement root=doc.DocumentElement;
                if(!Directory.Exists(Path.GetDirectoryName(XmlFilePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(XmlFilePath));
                }
                doc.Save(XmlFilePath);
                return true;
            }catch(Exception ee)
            {
                HelperLog = ee.Message;
            }
            return false;
        }
        /// <summary>
        /// 插入操作
        /// </summary>
        /// <param name="XmlFilePath">xml文档路径</param>
        /// <param name="NodePath">XPath 节点的父路径,为空表示父节点为根节点</param>
        /// <param name="NodeName">新节点名称</param>
        /// <param name="innerText">新节点innerText值</param>
        /// <param name="attributes">新节点属性</param>
        /// <returns></returns>
        public bool Insert(string XmlFilePath, string NodePath, string NodeName, string innerText, IDictionary<string, string> attributes)
        {
            HelperLog = "";
            if (!File.Exists(XmlFilePath))
            {
                HelperLog = "xml文件不存在";
                return false;
            }
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(XmlFilePath);

                XmlNode node = null;
                if (String.IsNullOrEmpty(NodePath))
                {
                    node = doc.DocumentElement as XmlNode;
                }
                else
                {
                    node = doc.SelectSingleNode(NodePath);
                }
                if (node == null)
                {
                    HelperLog = "节点不存在";
                    return false;
                }

                XmlElement newNode = doc.CreateElement(NodeName);
                newNode.InnerText = innerText;
                if (attributes != null && attributes.Count > 0)
                {
                    foreach (string key in attributes.Keys)
                    {
                        string value = attributes[key];
                        newNode.SetAttribute(key, value);
                    }
                }
                node.AppendChild(newNode);
                doc.Save(XmlFilePath);
            }
            catch (Exception ee)
            {
                HelperLog = ee.Message;
            }
            return false;
        }
        /// <summary>
        /// 读取操作
        /// </summary>
        /// <param name="XmlFilePath">xml文档路径</param>
        /// <param name="NodePath">XPath 节点的父路径,为空表示父节点为根节点</param>
        /// <param name="attribute">节点属性,为空则返回节点innerText</param>
        /// <returns></returns>
        public string Read(string XmlFilePath, string NodePath,string attribute)
        {
            HelperLog = "";
            if (!File.Exists(XmlFilePath))
            {
                HelperLog = "xml文件不存在";
                return "";
            }
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(XmlFilePath);

                XmlNode node = null;
                if (String.IsNullOrEmpty(NodePath))
                {
                    node = doc.DocumentElement as XmlNode;
                }
                else
                {
                    node = doc.SelectSingleNode(NodePath);
                }
                if (node == null)
                {
                    HelperLog = "节点不存在";
                    return "";
                }
                if (!String.IsNullOrEmpty(attribute))
                {
                    if (node.Attributes != null && node.Attributes.Count >= 0)
                    {
                        if (node.Attributes[attribute] != null)
                        {
                            return node.Attributes[attribute].Value;
                        }
                    }
                }
                else
                {
                    return node.InnerText;
                }
                return "";
            }
            catch (Exception ee)
            {
                HelperLog = ee.Message;
            }
            return "";
        }

        public bool Update(string XmlFilePath, string NodePath, string newNodeName, string newinnerText, IDictionary<string, string> newattributes)
        {
            HelperLog = "";
            if (!File.Exists(XmlFilePath))
            {
                HelperLog = "xml文件不存在";
                return false;
            }
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(XmlFilePath);

                XmlNode node = null;
                if (String.IsNullOrEmpty(NodePath))
                {
                    node = doc.DocumentElement as XmlNode;
                }
                else
                {
                    node = doc.SelectSingleNode(NodePath);
                }
                if (node == null)
                {
                    HelperLog = "节点不存在";
                    return false;
                }
               
                XmlElement xe = node as XmlElement;
               
                if (newNodeName != null)
                {
                 
                    //node.Name = newNodeName;
                }
                if (newinnerText != null)
                {
                    node.InnerText = newinnerText;
                }
 
                if (newattributes != null && newattributes.Count > 0)
                {
                    foreach (string key in newattributes.Keys)
                    {
                        string value = newattributes[key];
                        xe.SetAttribute(key, value);
                    }
                }
                doc.Save(XmlFilePath);
                return true;
            }
            catch (Exception ee)
            {
                HelperLog = ee.Message;
            }
            return false;
        }

        
        public bool Delete(string XmlFilePath,string NodePath)
        {
            HelperLog = "";
            if (!File.Exists(XmlFilePath))
            {
                HelperLog = "xml文件不存在";
                return false;
            }
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(XmlFilePath);

                XmlNode node = doc.SelectSingleNode(NodePath);
                if (node == null)
                {
                    HelperLog = "节点不存在";
                    return false;
                }
                node.ParentNode.RemoveChild(node);
                doc.Save(XmlFilePath);
                return true;
            }
            catch (Exception ee)
            {
                HelperLog = ee.Message;
            }
            return false;
        }
    }
}

原创粉丝点击