C# xml文件操作

   工作中需要用到一些xml的操作,一般最多是二级元素,所以索性自己封装了一个操作xml的类。

代码:

    class MyXml
    {
        private XmlDocument _xml = new XmlDocument();
        private string _filename = null;
        private string InitStr = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> \n<xml>\n</xml> ";

        /// <summary>
        /// 构造函数,输入参数为所谓载入的xml文件名
        /// 如果文件不存在,则创建一个新文件
        /// </summary>
        /// <param name="filename"></param>
        public MyXml(string filename)
        {
            try
            {
                _xml.Load(filename);
            }
            catch
            {
                if (!System.IO.File.Exists(filename)) //文件不存在,创建文件
                {
                    StreamWriter file = File.CreateText(filename);
                    file.Write(InitStr);
                    file.Close();
                    _xml.Load(filename);
                }
            }
            finally
            {
                _filename = filename;
            }
        }

        /// <summary>
        /// 生成一个节点,pNode为节点名
        /// </summary>
        /// <param name="pNode"></param>
        public void CreatePNode(string pNode)
        {
            try
            {
                XmlNode root = _xml.SelectSingleNode("xml").SelectSingleNode(pNode);
                if (root == null)
                {
                    root = _xml.CreateElement(pNode);
                    _xml.DocumentElement.AppendChild(root);
                }
            }
            catch { }
        }

        /// <summary>
        /// 设置元素值
        /// </summary>
        /// <param name="pNode">节点</param>
        /// <param name="cNode">元素</param>
        /// <param name="value">元素值</param>
        public void SetSecondAttr(string pNode, string cNode, string value)
        {
            try
            {
                XmlNode root = _xml.SelectSingleNode("xml").SelectSingleNode(pNode);
                if (root == null)
                {
                    root = _xml.CreateElement(pNode);
                    XmlElement node = _xml.CreateElement(cNode);
                    node.InnerText = value;
                    root.AppendChild(node);
                    _xml.DocumentElement.AppendChild(root);
                }
                else
                {
                    XmlNode node = root.SelectSingleNode(cNode);
                    if (node == null)
                    {
                        XmlElement node2 = _xml.CreateElement(cNode);
                        node2.InnerText = value;
                        root.AppendChild(node2);
                    }
                    else
                    {
                        node.InnerText = value;
                    }
                }

                _xml.Save(_filename);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 获取元素值
        /// </summary>
        /// <param name="pNode">节点</param>
        /// <param name="cNode">元素</param>
        /// <returns>元素值</returns>
        public string GetElementValue(string pNode, string cNode)
        {
            try
            {
                XmlNode node = _xml.SelectSingleNode("xml").
                        SelectSingleNode(pNode).SelectSingleNode(cNode);
                if (node == null)
                {
                    return null;
                }
                else
                {
                    return node.InnerText;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="Node"></param>
        public void RemoveNode(string Node)
        {
            try
            {
                XmlNode root = _xml.SelectSingleNode("xml").SelectSingleNode(Node);
                if (root != null)
                {
                    _xml.DocumentElement.RemoveChild(root);
                    _xml.Save(_filename);
                }
            }
            catch(Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 删除元素
        /// </summary>
        /// <param name="pNode">节点</param>
        /// <param name="Element">元素</param>
        public void RemoveElement(string pNode,string Element)
        {
            try
            {
               
                XmlNode root = _xml.SelectSingleNode("xml").SelectSingleNode(pNode);
                if (root != null)
                {
                    XmlNode element = root.SelectSingleNode(Element);
                    root.RemoveChild(element);
                    _xml.Save(_filename);
                }
            }
            catch { }
        }
    }





猜你喜欢

转载自blog.csdn.net/fenglifeng1987/article/details/43019039