using System.Xml;

namespace CallCenter.Utility
{
    /// <summary>
    /// C#XML文件操作类 包含常用XML操作信息
    /// by CrazyCoder.Cn
    /// 转载请注明出处
    /// </summary>
    public class XmlHelper
    {
        protected XmlDocument xdoc = new XmlDocument();
        public XmlElement root;

        #region LoadXml
        /// <summary>
        /// 加载Xml文档
        /// </summary>
        /// <param name="xml"></param>
        public void LoadXml(string xml)
        {
            xdoc.Load(xml);

        }

        /// <summary>
        /// 获取XML文件内容
        /// </summary>
        /// <param name="xml"></param>
        public string InnerXml(string xml)
        {
            xdoc.Load(xml);
            return xdoc.InnerXml;
        }
        /// <summary>
        /// 获取XML文件内容
        /// </summary>
        /// <param name="xml"></param>
        public void LoadFromXmlString(string xml)
        {
            xdoc.LoadXml(xml);
        }
        #endregion

        #region 创建一个节点，并在该节点下加入子节点

        /// <summary>
        /// 创建一个节点，并在该节点下加入子节点
        /// </summary>
        /// <param name="RootNode"></param>
        /// <param name="ElementName"></param>
        /// <param name="ChildName"></param>
        /// <param name="ChildValue"></param>
        /// <returns></returns>
        public void CreateElement(string RootNode, string ElementName, string[] ChildName, string[] ChildValue)
        {
            XmlNode Root = xdoc.SelectSingleNode(RootNode);
            XmlElement Node = xdoc.CreateElement(ElementName);
            if (ChildName.Length == ChildValue.Length)
            {
                for (int i = 0; i < ChildName.Length; i++)
                {
                    XmlElement ChildNode = xdoc.CreateElement(ChildName[i]);
                    ChildNode.InnerText = ChildValue[i];
                    Node.AppendChild(ChildNode);
                }
            }
            if (Root != null) Root.AppendChild(Node);
        }
        #endregion

        #region 删除某一节点

        /// <summary>
        /// 创建一个节点，并在该节点下加入子节点
        /// </summary>
        /// <param name="RootNode"></param>
        /// <param name="ChildName"></param>
        /// <param name="ChildValue"></param>
        /// <returns></returns>
        public void DeleteElement(string RootNode, string ChildName, string ChildValue)
        {
            XmlNode Root = xdoc.SelectSingleNode(RootNode);
            if (Root != null)
            {
                XmlNodeList Node = Root.ChildNodes;
                foreach (XmlNode xn in Node)
                {
                    var selectSingleNode = xn.SelectSingleNode(ChildName);
                    if (selectSingleNode != null && selectSingleNode.InnerText == ChildValue)
                    {
                        Root.RemoveChild(xn);
                    }
                }
            }
        }
        #endregion

        #region 编辑某一节点

        /// <summary>
        /// 创建一个节点，并在该节点下加入子节点
        /// </summary>
        /// <returns></returns>
        public void UpdateElement(string RootNode, string ElementName, string ElementValue, string[] ChildName, string[] ChildValue)
        {
            XmlNode Root = xdoc.SelectSingleNode(RootNode);
            if (Root != null)
            {
                XmlNodeList Node = Root.ChildNodes;
                foreach (XmlNode xn in Node)
                {
                    var selectSingleNode = xn.SelectSingleNode(ElementName);
                    if (selectSingleNode != null && selectSingleNode.InnerText == ElementValue)
                    {
                        if (ChildName.Length == ChildValue.Length)
                        {
                            for (int i = 0; i < ChildName.Length; i++)
                            {
                                var singleNode = xn.SelectSingleNode(ChildName[i]);
                                if (singleNode != null)
                                    singleNode.InnerText = ChildValue[i];
                            }
                        }
                        break;
                    }
                }
            }
        }

        public void UpdateElement(string RootNode, string[] ChildName, string[] ChildValue)
        {
            XmlNode Root = xdoc.SelectSingleNode(RootNode);
            if (ChildName.Length == ChildValue.Length)
            {
                for (int i = 0; i < ChildName.Length; i++)
                {
                    if (Root != null)
                    {
                        var selectSingleNode = Root.SelectSingleNode(ChildName[i]);
                        if (selectSingleNode != null)
                            selectSingleNode.InnerText = ChildValue[i];
                    }
                }
            }

        }


        public void UpdateElement(string RootNode, string NodeName, string[] ChildName, string[] ChildValue)
        {
            XmlNode Root = xdoc.SelectSingleNode(RootNode);
            if (Root != null)
            {
                XmlNode Node = Root.SelectSingleNode(NodeName);
                if (ChildName.Length == ChildValue.Length)
                {
                    for (int i = 0; i < ChildName.Length; i++)
                    {
                        if (Node != null)
                        {
                            var selectSingleNode = Node.SelectSingleNode(ChildName[i]);
                            if (selectSingleNode != null)
                                selectSingleNode.InnerText = ChildValue[i];
                        }
                    }
                }
            }
        }

        #endregion

        #region 取得名称为name的结点的值
        /// <summary>
        /// 取得名称为name的结点的值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetValue(string name)
        {
            if (xdoc.DocumentElement != null)
            {
                XmlNode xn = FindXnByName(xdoc.DocumentElement.ChildNodes, name);
                if (xn == null) return null;
                return xn.InnerText;
            }
            return null;
        }

        #endregion

        #region 在某节点下取名称为name的结点的值
        /// <summary>
        /// 在某节点下取名称为name的结点的值
        /// </summary>
        /// <returns></returns>
        public string GetValue(string NodeName, string name)
        {
            string BackString = "";
            if (xdoc.DocumentElement != null)
            {
                XmlNode xn = FindXnByName(xdoc.DocumentElement.ChildNodes, NodeName);
                if (xn == null)
                {
                    return "";
                }
                XmlNode nn = FindXnByName(xn.ChildNodes, name);
                if (nn == null)
                {
                    return "";
                }
                BackString = nn.InnerText;
            }
            return BackString;
        }
        #endregion

        #region 通过节点名称找到指定的节点
        /// <summary>
        /// 通过节点名称找到指定的节点
        /// </summary>
        /// <param name="xnl"></param>
        /// <param name="strName"></param>
        /// <returns></returns>
        protected XmlNode FindXnByName(XmlNodeList xnl, string strName)
        {
            for (int i = 0; i < xnl.Count; i++)
            {
                var item = xnl.Item(i);
                if (item != null && item.LocalName == strName)
                {
                    return xnl.Item(i);
                }
            }
            return null;
        }
        #endregion

        #region 为一个节点指定值
        /// <summary>
        /// 为一个节点指定值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="strValue"></param>
        public void SetValue(string strName, string strValue)
        {
            if (xdoc.DocumentElement != null)
            {
                XmlNodeList xnl = xdoc.DocumentElement.ChildNodes;
                for (int i = 0; i < xnl.Count; i++)
                {
                    var xmlNode = xnl.Item(i);
                    if (xmlNode != null && xmlNode.LocalName == strName)
                    {
                        var item = xnl.Item(i);
                        if (item != null) item.InnerText = strValue;
                        return;
                    }
                }
            }
            return;
        }
        #endregion

        #region 保存XML文档
        /// <summary>
        ///  保存XML文档
        /// </summary>
        public void Save(string name)
        {
            xdoc.Save(name);
        }
        #endregion
    }
}

