专注

导航

操作XML的类-XmlHelper

   1:          #region 公共变量
   2:          XmlDocument xmldoc;
   3:          XmlNode xmlnode;
   4:          XmlElement xmlelem;
   5:          #endregion
   6:   
   7:          #region 创建Xml文档
   8:          /// <summary>
   9:          /// 创建一个带有根节点的Xml文件
  10:          /// </summary>
  11:          /// <param name="FileName">Xml文件名称</param>
  12:          /// <param name="rootName">根节点名称</param>
  13:          /// <param name="Encode">编码方式:gb2312,UTF-8等常见的</param>
  14:          /// <param name="DirPath">保存的目录路径</param>
  15:          /// <returns></returns>
  16:          public bool CreateXmlDocument(string FileName, string rootName, string Encode)
  17:          {
  18:              try
  19:              {
  20:                  xmldoc = new XmlDocument();
  21:                  XmlDeclaration xmldecl;
  22:                  xmldecl = xmldoc.CreateXmlDeclaration("1.0", Encode, null);
  23:                  xmldoc.AppendChild(xmldecl);
  24:                  xmlelem = xmldoc.CreateElement("", rootName, "");
  25:                  xmldoc.AppendChild(xmlelem);
  26:                  xmldoc.Save(FileName);
  27:                  return true;
  28:              }
  29:              catch (Exception e)
  30:              {
  31:                  return false;
  32:                  throw new Exception(e.Message);
  33:              }
  34:          }
  35:   
  36:          #endregion
  37:   
  38:          #region 常用操作方法(增删改)
  39:          /// <summary>
  40:          /// 插入一个节点和它的若干子节点
  41:          /// </summary>
  42:          /// <param name="XmlFile">Xml文件路径</param>
  43:          /// <param name="NewNodeName">插入的节点名称</param>
  44:          /// <param name="HasAttributes">此节点是否具有属性,True为有,False为无</param>
  45:          /// <param name="fatherNode">此插入节点的父节点</param>
  46:          /// <param name="htAtt">此节点的属性,Key为属性名,Value为属性值</param>
  47:          /// <param name="htSubNode">子节点的属性,Key为Name,Value为InnerText</param>
  48:          /// <returns>返回真为更新成功,否则失败</returns>
  49:          public bool InsertNode(string XmlFile, string NewNodeName, bool HasAttributes, string fatherNode, Hashtable htAtt, Hashtable htSubNode)
  50:          {
  51:              try
  52:              {
  53:                  xmldoc = new XmlDocument();
  54:                  xmldoc.Load(XmlFile);
  55:                  XmlNode root = xmldoc.SelectSingleNode(fatherNode);
  56:                  xmlelem = xmldoc.CreateElement(NewNodeName);
  57:   
  58:                  if (htAtt != null && HasAttributes)//若此节点有属性,则先添加属性
  59:                  {
  60:                      SetAttributes(xmlelem, htAtt);
  61:   
  62:                      SetNodes(xmlelem.Name, xmldoc, xmlelem, htSubNode);//添加完此节点属性后,再添加它的子节点和它们的InnerText
  63:   
  64:                  }
  65:                  else
  66:                  {
  67:                      SetNodes(xmlelem.Name, xmldoc, xmlelem, htSubNode);//若此节点无属性,那么直接添加它的子节点
  68:                  }
  69:   
  70:                  root.AppendChild(xmlelem);
  71:                  xmldoc.Save(XmlFile);
  72:   
  73:                  return true;
  74:              }
  75:              catch (Exception e)
  76:              {
  77:   
  78:                  throw new Exception(e.Message);
  79:   
  80:              }
  81:          }
  82:          /// <summary>
  83:          /// 更新节点
  84:          /// </summary>
  85:          /// <param name="XmlFile">Xml文件路径</param>
  86:          /// <param name="fatherNode">需要更新节点的上级节点</param>
  87:          /// <param name="htAtt">需要更新的属性表,Key代表需要更新的属性,Value代表更新后的值</param>
  88:          /// <param name="htSubNode">需要更新的子节点的属性表,Key代表需要更新的子节点名字Name,Value代表更新后的值InnerText</param>
  89:          /// <returns>返回真为更新成功,否则失败</returns>
  90:          public bool UpdateNode(string XmlFile, string fatherNode, Hashtable htAtt, Hashtable htSubNode)
  91:          {
  92:              try
  93:              {
  94:                  xmldoc = new XmlDocument();
  95:                  xmldoc.Load(XmlFile);
  96:                  XmlNodeList root = xmldoc.SelectSingleNode(fatherNode).ChildNodes;
  97:                  UpdateNodes(root, htAtt, htSubNode);
  98:                  xmldoc.Save(XmlFile);
  99:                  return true;
 100:              }
 101:              catch (Exception e)
 102:              {
 103:                  throw new Exception(e.Message);
 104:              }
 105:          }
 106:   
 107:          /// <summary>
 108:          /// 删除指定节点下的子节点
 109:          /// </summary>
 110:          /// <param name="XmlFile">Xml文件路径</param>
 111:          /// <param name="fatherNode">制定节点</param>
 112:          /// <returns>返回真为更新成功,否则失败</returns>
 113:          public bool DeleteNodes(string XmlFile, string fatherNode)
 114:          {
 115:              try
 116:              {
 117:                  xmldoc = new XmlDocument();
 118:                  xmldoc.Load(XmlFile);
 119:                  xmlnode = xmldoc.SelectSingleNode(fatherNode);
 120:                  xmlnode.RemoveAll();
 121:                  xmldoc.Save(XmlFile);
 122:                  return true;
 123:              }
 124:              catch (XmlException xe)
 125:              {
 126:                  throw new XmlException(xe.Message);
 127:              }
 128:          }
 129:          #endregion
 130:   
 131:          #region 私有方法
 132:          /// <summary>
 133:          /// 设置节点属性
 134:          /// </summary>
 135:          /// <param name="xe">节点所处的Element</param>
 136:          /// <param name="htAttribute">节点属性,Key代表属性名称,Value代表属性值</param>
 137:          private void SetAttributes(XmlElement xe, Hashtable htAttribute)
 138:          {
 139:              foreach (DictionaryEntry de in htAttribute)
 140:              {
 141:                  xe.SetAttribute(de.Key.ToString(), de.Value.ToString());
 142:              }
 143:          }
 144:          /// <summary>
 145:          /// 增加子节点到根节点下
 146:          /// </summary>
 147:          /// <param name="rootNode">上级节点名称</param>
 148:          /// <param name="XmlDoc">Xml文档</param>
 149:          /// <param name="rootXe">父根节点所属的Element</param>
 150:          /// <param name="SubNodes">子节点属性,Key为Name值,Value为InnerText值</param>
 151:          private void SetNodes(string rootNode, XmlDocument XmlDoc, XmlElement rootXe, Hashtable SubNodes)
 152:          {
 153:              foreach (DictionaryEntry de in SubNodes)
 154:              {
 155:                  xmlnode = XmlDoc.SelectSingleNode(rootNode);
 156:                  XmlElement subNode = XmlDoc.CreateElement(de.Key.ToString());
 157:                  subNode.InnerText = de.Value.ToString();
 158:                  rootXe.AppendChild(subNode);
 159:              }
 160:          }
 161:          /// <summary>
 162:          /// 更新节点属性和子节点InnerText值
 163:          /// </summary>
 164:          /// <param name="root">根节点名字</param>
 165:          /// <param name="htAtt">需要更改的属性名称和值</param>
 166:          /// <param name="htSubNode">需要更改InnerText的子节点名字和值</param>
 167:          private void UpdateNodes(XmlNodeList root, Hashtable htAtt, Hashtable htSubNode)
 168:          {
 169:              foreach (XmlNode xn in root)
 170:              {
 171:                  xmlelem = (XmlElement)xn;
 172:                  if (xmlelem.HasAttributes)//如果节点如属性,则先更改它的属性
 173:                  {
 174:                      foreach (DictionaryEntry de in htAtt)//遍历属性哈希表
 175:                      {
 176:                          if (xmlelem.HasAttribute(de.Key.ToString()))//如果节点有需要更改的属性
 177:                          {
 178:                              xmlelem.SetAttribute(de.Key.ToString(), de.Value.ToString());//则把哈希表中相应的值Value赋给此属性Key
 179:                          }
 180:                      }
 181:                  }
 182:                  if (xmlelem.HasChildNodes)//如果有子节点,则修改其子节点的InnerText
 183:                  {
 184:                      XmlNodeList xnl = xmlelem.ChildNodes;
 185:                      foreach (XmlNode xn1 in xnl)
 186:                      {
 187:                          XmlElement xe = (XmlElement)xn1;
 188:                          foreach (DictionaryEntry de in htSubNode)
 189:                          {
 190:                              if (xe.Name == de.Key.ToString())//htSubNode中的key存储了需要更改的节点名称,
 191:                              {
 192:                                  xe.InnerText = de.Value.ToString();//htSubNode中的Value存储了Key节点更新后的数据
 193:                              }
 194:                          }
 195:                      }
 196:                  }
 197:   
 198:              }
 199:          }
 200:          #endregion
 201:      }

posted on 2011-11-13 11:40  陈啊M  阅读(289)  评论(0编辑  收藏  举报