XmlDocument doc = null;
        System.Xml.XmlNode node;
        string fileName = string.Empty;//文件路径+ 名称
        private string _errorInfo;//错误的友好信息 -给用户见的
        public string ErrorInfo
        {
            get { return _errorInfo; }
        }
        #region  构造函数                
        public XmlHelp(string filep)
        {
            fileName = filep;
            LoadXml();
        }
        public XmlHelp(string fileP,string root,string content)
        {
            fileName = fileP;
            CreateSampleXmlFile(root, content);
        }
        #endregion        #region 加载 保存 创建
        /// <summary>
        /// 加载xml文件
        /// </summary> 
        private void LoadXml()
        {
            doc = new XmlDocument();
            try
            {
                doc.Load(fileName);
            }
            catch
            {
                if (File.Exists(fileName))
                    _errorInfo = "文件加载失败";
                else
                    _errorInfo = "文件路径不正确";
                throw;
            }
        }
        public void CreateSampleXmlFile(string root,string content)
        {
            doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "gb2312", null));//设置xml的版本,格式信息
            XmlNode rn = doc.CreateElement("", root, "");
            rn.InnerText = content;
            doc.AppendChild(rn);//创建根元素
        }
        public void SaveXml()
        {
            if (doc != null)
            {
                try
                {
                    doc.Save(fileName);
                }
                catch
                {
                    _errorInfo = "文件保存失败,可能是路径不对或者是没有修改权限";
                    throw;
                }
                finally
                {
                    doc = null;
                }
            }
            else { _errorInfo = "文件保存失败,可能是文件路径不对或者是对文件没有权限"; }
        }
        #endregion        #region 查找节点 根节点 父节 子节点     
        public Object GetRootElement()//XmlElement
        {
            return doc.DocumentElement;
        }
        public Object GetXmlNode(string XPath)//XmlNode
        {
            node = doc.SelectSingleNode(XPath);
            if (node != null)
                return node;
            else
            {
                _errorInfo = "未找到"+XPath+"的节点";
                return null;
            }
        }
        public Object GetNodeChildNode(string XPath)//XmlNodeList
        {
            GetXmlNode(XPath);
            if (node != null)
                return node.ChildNodes;
            else
                return null;
        }
        public Object[] GetNodeChildNodes(string xPath)
        {
            GetXmlNode(xPath);
            Object[] arrNds = null;
            if (node != null)
            {
                XmlNodeList ndlts = node.ChildNodes;
                int count = ndlts.Count;
                if (count > 0)
                {
                    arrNds = new object[count];
                    for (int i = 0; i < count; i++)
                    {
                        arrNds[i] = ndlts[i];
                    }
                }
            }
            return arrNds;
        }
        public int GetNodeListsMaxValue(string xPath, bool isAttr,string attr)
        {
            int id = 0, lid=0 ;
           XmlNodeList nlist=(XmlNodeList) GetNodeChildNode(xPath);
           try
           {
               for (int i = 0; i < nlist.Count; i++)
               {
                   if (isAttr)
                       lid = Int32.Parse(nlist[i].Attributes[attr].Value);
                   else
                       lid = Int32.Parse(nlist[i].InnerText);
                   if (lid > id)
                       id = lid;
               }
           }
           catch { _errorInfo = "查找失败,类型转换失败,请属性或者是innerText的数据类型"; throw; }
           return id;
        }
        public Object GetParentNode(Object node)//XmlNode
        {
            XmlNode xnode = (XmlNode)node;
            XmlNode pnode = null;
            try
            {
                pnode = xnode.ParentNode;
            }
            catch
            {
                _errorInfo = "查找父节点失败,请检查" + xnode.Name + "节点";
                throw;
            }
            return pnode;
        }
        #endregion

解决方案 »

  1.   


     #region 获取值
            public string GetXmlNodeValue(string XPath)
            {
                GetXmlNode(XPath);
                if (node != null)
                    return node.InnerText;
                else
                    return null;
            }
            public string GetXmlNodeValue(Object xNode)
            {
                XmlNode nd = (XmlNode)xNode;
                return node.InnerText;
            }
            public string GetXmlNodeAtrribut(string XPath, string attr)
            {
                GetXmlNode(XPath);
                string attribute = string.Empty;
                if (node != null)
                {
                    try
                    {
                         attribute = node.Attributes[attr].Value;
                    }
                    catch
                    {
                        _errorInfo = "该节点不存在" + attr + "属性";
                        throw;
                    }
                }
                return attribute;
            }
            public string GetXmlNodeAtrribut(Object xNode, string attr)
            {
                string value = string.Empty;
                XmlNode nd = (XmlNode)xNode;
                try
                {
                    value = nd.Attributes[attr].Value;
                }
                catch { _errorInfo = "该节点不存在" + attr + "属性"; throw; }
                return value;
            }
            #endregion
            #region 更新 XmlNode
            public bool UpdateXmlNode(string XPath, string value)
            {
                GetXmlNode(XPath);
                if (node != null)
                {
                    node.InnerText = value;//执行后这个函数后记得调用保存函数
                    return true;
                }
                else
                    return false;
            }
            public bool UpdateXmlNode(string XPath, string content, string[] attri, string[] values)
            {
                GetXmlNode(XPath);
                if (node != null)
                {
                    node.InnerText = content;
                    int length = attri.Length;
                    for (int i = 0; i < length; i++)
                    {
                        node.Attributes[attri[i]].InnerText = values[i];
                    }
                    return true;
                }
                else
                    return false;
            }
            public bool UpdateXmlNodeAttribute(string XPath, string attr,string content)
            {
                GetXmlNode(XPath);
                if (node != null)
                {
                    node.Attributes[attr].InnerText = content;//执行后这个函数后记得调用保存函数
                    return true;
                }
                else
                    return false;
            }
            public void ReplaceNode(Object node, Object del_Node, Object new_Node)
            {
                XmlNode pnode = (XmlNode)node;
                XmlNode delNode = (XmlNode)del_Node;
                XmlNode newNode = (XmlNode)new_Node;
                 pnode.ReplaceChild(newNode, delNode);
            }
            #endregion        #region 创建节点
            public Object CreateNewXmlNode(string xnode, string innerText)//XmlNode
            {
                XmlNode node = doc.CreateElement(xnode);
                node.InnerText = innerText;
                return node;
            }
            public Object CreateAttribute(string attr, string value)//XmlAttribute
            {
                XmlAttribute att = doc.CreateAttribute(attr);
                att.Value = value;
                return att;
            }
            #endregion        #region 插入
            public void AppendChild(Object node, Object root_Node)
            {
                XmlNode xnode = (XmlNode)node;
                XmlNode rootNode = (XmlNode)root_Node;
                try
                {
                    rootNode.AppendChild(xnode);
                }
                catch 
                {
                    _errorInfo = "给父节点中添加子节点失败`";
                    throw;
                }
            }
            public void AppendChildAttr(Object Attr, Object root_Node)
            {
                XmlAttribute xAttr = (XmlAttribute)Attr;
                XmlNode rootNode = (XmlNode)root_Node;
                try
                {
                    rootNode.Attributes.Append(xAttr);
                }
                catch
                {
                    _errorInfo = "给节点添加属性时出错";
                    throw;
                }
            }
            public void AppendChild(string pnode, string name, string content)
            {
                XmlNode pNode, node;
                pNode = (XmlNode)GetXmlNode(pnode);
                node = (XmlNode)CreateNewXmlNode(name, content);
                AppendChild(node, pNode);
            }
            public void AppendAttribute(string pnode, string att, string value)
            {
                node = (XmlNode)GetXmlNode(pnode);
                XmlAttribute attri = (XmlAttribute)CreateAttribute(att, value);
                AppendChild(attri, node);
            }
            public void AppendAttribute(string pnode, string[] att, string[] values)
            {
                int length=att.Length;
                if (length!= values.Length)
                    return;
                node = (XmlNode)GetXmlNode(pnode);
                 XmlAttribute attri;
                for (int i = 0; i < length; i++)
                {
                    attri = (XmlAttribute)CreateAttribute(att[i], values[i]);
                    AppendChild(attri, node);
                }              
            }
            #endregion      
            #region 删除
            public bool DelNode(Object p_node, Object _node)
            {
                XmlNode pnode = (XmlNode)p_node; 
                XmlNode node = (XmlNode)_node;
                try
                {
                    pnode.RemoveChild(node);
                    return true;
                }
                catch
                {
                    _errorInfo = "从节点"+pnode .Name+"删除"+node .Name +"失败";
                    throw;
                }
            }
            public bool DelNode(string Node)
            {
                string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
                XmlNode pnode,node;
                try
                {
                    pnode = (XmlNode)GetXmlNode(mainNode);
                    node = (XmlNode)GetXmlNode(Node);
                }
                catch {
                    throw ;
                }
              return  DelNode(pnode, node);
            }
            #endregion        #region 返回一个 DateView        public DataView GetDate(string XPath)
            {
                DataSet ds = new DataSet();
                GetXmlNode(XPath);
                if (node != null)
                {
                    StringReader read = new StringReader(node.OuterXml);
                    ds.ReadXml(read);
                    if (ds.Tables.Count > 0)
                        return ds.Tables[0].DefaultView;
                    else
                        return null;
                }
                else
                    return null;
            }
            #endregion    }
      

  2.   

    不建议用XmlNodeList去查大的xml,建议使用流模式去读写xml.
      

  3.   

    我看BlogEngine.NET  就是才用流的方式
       不知道这样好处在那 另外 操作xml文件 一般都要对文件或者文件夹有相应的权限  , 用流方式在这方面是否需要  我记得修改webconfig  有的方法不需要文件权限 不过方式不一样  ,好象通过的另外的一个命名空间的类实现的 
      

  4.   

    结构还要优化些,通过泛型操作节点内容
    http://www.cnblogs.com/aidydream/articles/1279451.html
      

  5.   

    public bool UpdateXmlNode(string XPath, string content, string[] attri, string[] values)
    key/value Dictionary<string,string>
      

  6.   


    stream好象也是读区整个文件进行操作啊.. 你是不是想说xpath?
      

  7.   


    原来也想过用 Dictionary <string,string>
     但是不知道在这里具体有什么好处
      

  8.   

    代码代码可读性,比如有20对key/value,2个数组,一个一个对那是很痛苦的一件事