1 /// <summary> 2 /// XML文檔操做工具類 3 /// </summary> 4 public class XmlUtil 5 { 6 #region XML文檔節點查詢和讀取 7 8 /// <summary> 9 /// 選擇匹配XPath表達式的第一個節點XmlNode. 10 /// </summary> 11 /// <param name="xmlDoc">XML文檔</param> 12 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 13 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名")</param> 14 /// <returns>返回XmlNode</returns> 15 public static XmlNode GetXmlNodeByXpath(XmlDocument xmlDoc, string xpath) 16 { 17 try 18 { 19 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); 20 return xmlNode; 21 } 22 catch (Exception ex) 23 { 24 throw ex; //這裏能夠定義你本身的異常處理 25 } 26 } 27 28 /// <summary> 29 /// 選擇匹配XPath表達式的節點列表XmlNodeList. 30 /// </summary> 31 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 32 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名")</param> 33 /// <returns>返回XmlNodeList</returns> 34 public static XmlNodeList GetXmlNodeListByXpath(XmlDocument xmlDoc, string xpath) 35 { 36 try 37 { 38 XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath); 39 return xmlNodeList; 40 } 41 catch (Exception ex) 42 { 43 throw ex; //這裏能夠定義你本身的異常處理 44 } 45 } 46 47 /// <summary> 48 /// 選擇匹配XPath表達式的第一個節點的匹配xmlAttributeName的屬性XmlAttribute. 49 /// </summary> 50 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 51 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名</param> 52 /// <param name="xmlAttributeName">要匹配xmlAttributeName的屬性名稱,默認爲value</param> 53 /// <returns>返回xmlAttributeName</returns> 54 public static XmlAttribute GetXmlAttribute(XmlDocument xmlDoc, string xpath, params string[] xmlAttributeName) 55 { 56 string content = string.Empty; 57 XmlAttribute xmlAttribute = null; 58 try 59 { 60 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); 61 if (xmlNode != null) 62 { 63 if (xmlNode.Attributes.Count > 0) 64 { 65 if (int.Equals(xmlAttributeName.Length, 0)) 66 xmlAttribute = xmlNode.Attributes["value"]; 67 else 68 xmlAttribute = xmlNode.Attributes[xmlAttributeName[0]]; 69 } 70 } 71 } 72 catch (Exception ex) 73 { 74 throw ex; //這裏能夠定義你本身的異常處理 75 } 76 return xmlAttribute; 77 } 78 79 #endregion 80 81 #region XML文檔建立和節點或屬性的添加、修改 82 83 /// <summary> 84 /// 建立一個XML文檔 85 /// </summary> 86 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 87 /// <param name="rootNodeName">XML文檔根節點名稱(須指定一個根節點名稱)</param> 88 /// <param name="version">XML文檔信息量(必須爲:"1.0")</param> 89 /// <param name="encoding">XML文檔編碼方式</param> 90 /// <param name="standalone">該值必須是"yes"或"no",若是爲null,Save方法不在XML聲明上寫出獨立屬性</param> 91 /// <returns>成功返回true,失敗返回false</returns> 92 public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone) 93 { 94 bool isSuccess = false; 95 try 96 { 97 XmlDocument xmlDoc = new XmlDocument(); 98 XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone); 99 XmlNode root = xmlDoc.CreateElement(rootNodeName); 100 xmlDoc.AppendChild(xmlDeclaration); 101 xmlDoc.AppendChild(root); 102 xmlDoc.Save(xmlFileName); 103 isSuccess = true; 104 } 105 catch (Exception ex) 106 { 107 throw ex; //這裏能夠定義你本身的異常處理 108 } 109 return isSuccess; 110 } 111 112 /// <summary> 113 /// 依據匹配XPath表達式的第一個節點來建立它的子節點(若是此節點已存在則追加一個新的同名節點 114 /// </summary> 115 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 116 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名</param> 117 /// <param name="xmlNodeName">要匹配xmlNodeName的節點名稱</param> 118 /// <param name="innerText">節點文本值</param> 119 /// <param name="xmlAttributeName">要匹配xmlAttributeName的屬性名稱集合</param> 120 /// <param name="value">屬性值集合</param> 121 /// <returns>成功返回true,失敗返回false</returns> 122 public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, string[] xmlAttributeName, string[] value) 123 { 124 bool isSuccess = false; 125 XmlDocument xmlDoc = new XmlDocument(); 126 try 127 { 128 xmlDoc.Load(xmlFileName); //加載XML文檔 129 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); 130 if (xmlNode != null) 131 { 132 //存不存在此節點都建立 133 XmlElement subElement = xmlDoc.CreateElement(xmlNodeName); 134 subElement.InnerXml = innerText; 135 136 //若是屬性和值參數都不爲空則在此新節點上新增屬性 137 if (xmlAttributeName != null && value != null) 138 { 139 for (int i = 0; i < xmlAttributeName.Length; i++) 140 { 141 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName[i]); 142 xmlAttribute.Value = value[i]; 143 subElement.Attributes.Append(xmlAttribute); 144 } 145 } 146 xmlNode.AppendChild(subElement); 147 } 148 xmlDoc.Save(xmlFileName); //保存到XML文檔 149 isSuccess = true; 150 } 151 catch (Exception ex) 152 { 153 throw ex; //這裏能夠定義你本身的異常處理 154 } 155 return isSuccess; 156 } 157 158 /// <summary> 159 /// 依據匹配XPath表達式的第一個節點來建立或更新它的子節點(若是節點存在則更新,不存在則建立) 160 /// </summary> 161 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 162 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名</param> 163 /// <param name="xmlNodeName">要匹配xmlNodeName的節點名稱</param> 164 /// <param name="innerText">節點文本值</param> 165 /// <returns>成功返回true,失敗返回false</returns> 166 public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText) 167 { 168 bool isSuccess = false; 169 bool isExistsNode = false;//標識節點是否存在 170 XmlDocument xmlDoc = new XmlDocument(); 171 try 172 { 173 xmlDoc.Load(xmlFileName); //加載XML文檔 174 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); 175 if (xmlNode != null) 176 { 177 //遍歷xpath節點下的全部子節點 178 foreach (XmlNode node in xmlNode.ChildNodes) 179 { 180 if (node.Name.ToLower() == xmlNodeName.ToLower()) 181 { 182 //存在此節點則更新 183 node.InnerXml = innerText; 184 isExistsNode = true; 185 break; 186 } 187 } 188 if (!isExistsNode) 189 { 190 //不存在此節點則建立 191 XmlElement subElement = xmlDoc.CreateElement(xmlNodeName); 192 subElement.InnerXml = innerText; 193 xmlNode.AppendChild(subElement); 194 } 195 } 196 xmlDoc.Save(xmlFileName); //保存到XML文檔 197 isSuccess = true; 198 } 199 catch (Exception ex) 200 { 201 throw ex; //這裏能夠定義你本身的異常處理 202 } 203 return isSuccess; 204 } 205 206 /// <summary> 207 /// 依據匹配XPath表達式的第一個節點來建立或更新它的屬性(若是屬性存在則更新,不存在則建立) 208 /// </summary> 209 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 210 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名</param> 211 /// <param name="xmlAttributeName">要匹配xmlAttributeName的屬性名稱</param> 212 /// <param name="value">屬性值</param> 213 /// <returns>成功返回true,失敗返回false</returns> 214 public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string value, params string[] xmlAttributeNamePara) 215 { 216 bool isSuccess = false; 217 bool isExistsAttribute = false;//標識屬性是否存在 218 XmlDocument xmlDoc = new XmlDocument(); 219 try 220 { 221 xmlDoc.Load(xmlFileName); //加載XML文檔 222 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); 223 if (xmlNode != null) 224 { 225 //默認爲value 226 string xmlAttributeName = "value"; 227 if (xmlAttributeNamePara.Length > 0) 228 { 229 xmlAttributeName = xmlAttributeNamePara[0]; 230 } 231 //遍歷xpath節點中的全部屬性 232 foreach (XmlAttribute attribute in xmlNode.Attributes) 233 { 234 if (attribute.Name.ToLower() == xmlAttributeName.ToLower()) 235 { 236 //節點中存在此屬性則更新 237 attribute.Value = value; 238 isExistsAttribute = true; 239 break; 240 } 241 } 242 if (!isExistsAttribute) 243 { 244 //節點中不存在此屬性則建立 245 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName); 246 xmlAttribute.Value = value; 247 xmlNode.Attributes.Append(xmlAttribute); 248 } 249 } 250 xmlDoc.Save(xmlFileName); //保存到XML文檔 251 isSuccess = true; 252 } 253 catch (Exception ex) 254 { 255 throw ex; //這裏能夠定義你本身的異常處理 256 } 257 return isSuccess; 258 } 259 260 #endregion 261 262 #region XML文檔節點或屬性的刪除 263 264 /// <summary> 265 /// 刪除匹配XPath表達式的第一個節點(節點中的子元素同時會被刪除) 266 /// </summary> 267 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 268 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名</param> 269 /// <returns>成功返回true,失敗返回false</returns> 270 public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath) 271 { 272 bool isSuccess = false; 273 XmlDocument xmlDoc = new XmlDocument(); 274 try 275 { 276 xmlDoc.Load(xmlFileName); //加載XML文檔 277 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); 278 if (xmlNode != null) 279 { 280 //刪除節點 281 xmlNode.ParentNode.RemoveChild(xmlNode); 282 } 283 xmlDoc.Save(xmlFileName); //保存到XML文檔 284 isSuccess = true; 285 } 286 catch (Exception ex) 287 { 288 throw ex; //這裏能夠定義你本身的異常處理 289 } 290 return isSuccess; 291 } 292 293 /// <summary> 294 /// 刪除匹配XPath表達式的第一個節點中的匹配參數xmlAttributeName的屬性 295 /// </summary> 296 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 297 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名</param> 298 /// <param name="xmlAttributeName">要刪除的xmlAttributeName的屬性名稱</param> 299 /// <returns>成功返回true,失敗返回false</returns> 300 public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName) 301 { 302 bool isSuccess = false; 303 bool isExistsAttribute = false; 304 XmlDocument xmlDoc = new XmlDocument(); 305 try 306 { 307 xmlDoc.Load(xmlFileName); //加載XML文檔 308 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); 309 XmlAttribute xmlAttribute = null; 310 if (xmlNode != null) 311 { 312 //遍歷xpath節點中的全部屬性 313 foreach (XmlAttribute attribute in xmlNode.Attributes) 314 { 315 if (attribute.Name.ToLower() == xmlAttributeName.ToLower()) 316 { 317 //節點中存在此屬性 318 xmlAttribute = attribute; 319 isExistsAttribute = true; 320 break; 321 } 322 } 323 if (isExistsAttribute) 324 { 325 //刪除節點中的屬性 326 xmlNode.Attributes.Remove(xmlAttribute); 327 } 328 } 329 xmlDoc.Save(xmlFileName); //保存到XML文檔 330 isSuccess = true; 331 } 332 catch (Exception ex) 333 { 334 throw ex; //這裏能夠定義你本身的異常處理 335 } 336 return isSuccess; 337 } 338 339 /// <summary> 340 /// 刪除匹配XPath表達式的第一個節點中的全部屬性 341 /// </summary> 342 /// <param name="xmlFileName">XML文檔徹底文件名(包含物理路徑)</param> 343 /// <param name="xpath">要匹配的XPath表達式(例如:"//節點名//子節點名</param> 344 /// <returns>成功返回true,失敗返回false</returns> 345 public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath) 346 { 347 bool isSuccess = false; 348 XmlDocument xmlDoc = new XmlDocument(); 349 try 350 { 351 xmlDoc.Load(xmlFileName); //加載XML文檔 352 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); 353 if (xmlNode != null) 354 { 355 //遍歷xpath節點中的全部屬性 356 xmlNode.Attributes.RemoveAll(); 357 } 358 xmlDoc.Save(xmlFileName); //保存到XML文檔 359 isSuccess = true; 360 } 361 catch (Exception ex) 362 { 363 throw ex; //這裏能夠定義你本身的異常處理 364 } 365 return isSuccess; 366 } 367 368 #endregion 369 }