1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Data; 5 using System.IO; 6 using System.Xml; 7 8 namespace Comm 9 { 10 /// <summary> 11 /// Xml类 12 /// </summary> 13 public class CustomXmlDocument 14 { 15 /// <summary> 16 /// 将Xml转成 dataset 17 /// </summary> 18 /// <param name="xmlfilepath">Xml文件全路径</param> 19 /// <returns>dataset</returns> 20 public static DataSet getDataset(string xmlfilepath) 21 { 22 bool flag = File.Exists(xmlfilepath); 23 if (!flag) 24 { 25 return null; 26 } 27 DataSet ds = new DataSet(); 28 try 29 { 30 ds.ReadXml(xmlfilepath); 31 } 32 catch 33 { 34 return null; 35 } 36 return ds; 37 } 38 39 /// <summary> 40 /// 将Xml转成 datatable 41 /// </summary> 42 /// <param name="xmlfilepath">Xml文件全路径</param> 43 /// <returns>datatable</returns> 44 public static DataTable getDatatable(string xmlfilepath) 45 { 46 DataSet ds = getDataset(xmlfilepath); 47 if (ds == null || ds.Tables.Count < 1) 48 { 49 return null; 50 } 51 return ds.Tables[0]; 52 } 53 54 /// <summary> 55 /// 获取 dataset 56 /// </summary> 57 /// <param name="xmlcontent">Xml内容</param> 58 /// <returns>dataset</returns> 59 public static DataSet getDatasetBycontent(string xmlcontent) 60 { 61 DataSet ds = null; 62 try 63 { 64 StringReader stringReader = new StringReader(xmlcontent); 65 XmlTextReader xmlTextReader = new XmlTextReader(stringReader); 66 ds = new DataSet(); 67 ds.ReadXmlSchema(xmlTextReader); 68 } 69 catch 70 { 71 return null; 72 } 73 74 return ds; 75 } 76 77 /// <summary> 78 /// 获取 datatable 79 /// </summary> 80 /// <param name="xmlcontent">Xml内容</param> 81 /// <returns>datatable</returns> 82 public static DataTable getDatatableBycontent(string xmlcontent) 83 { 84 DataSet ds = getDatasetBycontent(xmlcontent); 85 if (ds == null || ds.Tables.Count < 1) 86 { 87 return null; 88 } 89 90 return ds.Tables[0]; 91 } 92 93 /// <summary> 94 /// 将 xmlcontent 写成 Xml文件 95 /// </summary> 96 /// <param name="xmlcontent">Xml内容</param> 97 /// <returns>保存Xml全路径</returns> 98 public static string writeXmlByxmlcontent(string xmlcontent, string savefilepath) 99 { 100 try 101 { 102 File.WriteAllText(savefilepath, xmlcontent); 103 } 104 catch 105 { 106 return ""; 107 } 108 return savefilepath; 109 } 110 111 /// <summary> 112 /// 将 dataset 写成 Xml文件 113 /// </summary> 114 /// <param name="ds">dataset</param> 115 /// <param name="savefilepath">保存Xml全路径</param> 116 /// <returns>保存Xml全路径</returns> 117 public static string writeXmlBydataset(DataSet ds, string savefilepath) 118 { 119 string[] columns = new string[ds.Tables[0].Columns.Count]; 120 for (int i = 0; i < ds.Tables[0].Columns.Count; i++) 121 { 122 columns[i] = ds.Tables[0].Columns[i].ColumnName; 123 } 124 return writeXmlBydatatable(ds.Tables[0], savefilepath, columns); 125 } 126 127 /// <summary> 128 /// 将 datatable 写成 Xml文件 129 /// </summary> 130 /// <param name="dt">datatable</param> 131 /// <param name="savefilepath">保存Xml全路径</param> 132 /// <returns>保存Xml全路径</returns> 133 public static string writeXmlBydatatable(DataTable dt, string savefilepath,string[] columns) 134 { 135 try 136 { 137 System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); 138 XmlDeclaration xmldecl; 139 xmldecl = xmlDoc.CreateXmlDeclaration("1.0", null, null); 140 xmldecl.Encoding = "UTF-8"; 141 xmldecl.Standalone = "yes"; 142 XmlElement xmlElem = xmlDoc.CreateElement("table"); 143 xmlDoc.InsertBefore(xmldecl, xmlElem); 144 145 if (columns == null || columns.Length < 1) 146 { 147 return ""; 148 } 149 150 foreach (DataRow row in dt.Rows) 151 { 152 XmlElement xmlElemrow = xmlDoc.CreateElement("row"); 153 foreach (string column in columns) 154 { 155 XmlElement xmlElemcolum = xmlDoc.CreateElement(column); 156 xmlElemcolum.Value = row[column].ToString().Trim(); 157 xmlElemrow.AppendChild(xmlElemcolum); 158 } 159 xmlElem.AppendChild(xmlElemrow); 160 } 161 } 162 catch 163 { 164 return ""; 165 } 166 167 return savefilepath; 168 169 } 170 } 171 }
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Xml; 5 using System.Collections; 6 7 namespace Comm 8 { 9 public class XMLHelper 10 { 11 #region private AppendChild 12 private static void AppendChild(XmlDocument xDoc, XmlNode parentNode, params XmlParameter[] paras) 13 { 14 foreach (XmlParameter xpar in paras) 15 { 16 XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xpar.Name, null); 17 string ns = xpar.NamespaceOfPrefix == null ? "" : newNode.GetNamespaceOfPrefix(xpar.NamespaceOfPrefix); 18 foreach (AttributeParameter attp in xpar.Attributes) 19 { 20 XmlNode attr = xDoc.CreateNode(XmlNodeType.Attribute, attp.Name, ns == "" ? null : ns); 21 attr.Value = attp.Value; 22 newNode.Attributes.SetNamedItem(attr); 23 } 24 newNode.InnerText = xpar.InnerText; 25 parentNode.AppendChild(newNode); 26 } 27 } 28 #endregion 29 30 #region private AddEveryNode 31 private static void AddEveryNode(XmlDocument xDoc, XmlNode parentNode, params XmlParameter[] paras) 32 { 33 XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; 34 foreach (XmlNode xns in nlst) 35 { 36 if (xns.Name == parentNode.Name) 37 { 38 AppendChild(xDoc, xns, paras); 39 } 40 else 41 { 42 foreach (XmlNode xn in xns) 43 { 44 if (xn.Name == parentNode.Name) 45 { 46 AppendChild(xDoc, xn, paras); 47 } 48 } 49 } 50 } 51 } 52 #endregion 53 54 #region xmlDoc 55 /// <summary> 56 /// 创建一个XmlDocument对象 57 /// </summary> 58 /// <param name="PathOrString">文件名称或XML字符串</param> 59 public static XmlDocument xmlDoc(string PathOrString) 60 { 61 try 62 { 63 XmlDocument xDoc = new XmlDocument(); 64 if (System.IO.File.Exists(PathOrString)) 65 { 66 xDoc.Load(PathOrString); 67 } 68 else 69 { 70 xDoc.LoadXml(PathOrString); 71 } 72 return xDoc; 73 } 74 catch 75 { 76 return null; 77 } 78 } 79 #endregion 80 81 #region CreateXMLFile 82 /// <summary> 83 /// 创建一个XML文档 84 /// </summary> 85 /// <param name="fileFullName">文件名称,包括完整路径</param> 86 /// <param name="rootName">根结点名称</param> 87 /// <param name="elemName">元素节点名称</param> 88 /// <param name="paras">XML参数</param> 89 public static void CreateXMLFile(string fileFullName, string rootName, string elemName, params XmlParameter[] paras) 90 { 91 XmlDocument xDoc = new XmlDocument(); 92 XmlNode xn; 93 xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); 94 xDoc.AppendChild(xn); 95 XmlNode root = xDoc.CreateElement(rootName); 96 xDoc.AppendChild(root); 97 XmlNode ln = xDoc.CreateNode(XmlNodeType.Element, elemName, null); 98 AppendChild(xDoc, ln, paras); 99 root.AppendChild(ln); 100 try 101 { 102 xDoc.Save(fileFullName); 103 } 104 catch 105 { 106 throw; 107 } 108 } 109 /// <summary> 110 /// 创建一个XML文档 111 /// </summary> 112 /// <param name="fileFullName">文件名称,包括完整路径</param> 113 /// <param name="rootName">根结点名称</param> 114 /// <param name="elemp">元素节点对象</param> 115 /// <param name="paras">XML参数</param> 116 public static void CreateXMLFile(string fileFullName, string rootName, XmlParameter elemp, params XmlParameter[] paras) 117 { 118 XmlDocument xDoc = new XmlDocument(); 119 XmlNode xn; 120 xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); 121 xDoc.AppendChild(xn); 122 XmlNode root = xDoc.CreateElement(rootName); 123 xDoc.AppendChild(root); 124 XmlNode ln = xDoc.CreateNode(XmlNodeType.Element, elemp.Name, null); 125 string ns = elemp.NamespaceOfPrefix == null ? "" : ln.GetNamespaceOfPrefix(elemp.NamespaceOfPrefix); 126 foreach (AttributeParameter ap in elemp.Attributes) 127 { 128 XmlNode elemAtt = xDoc.CreateNode(XmlNodeType.Attribute, ap.Name, ns == "" ? null : ns); 129 elemAtt.Value = ap.Value; 130 ln.Attributes.SetNamedItem(elemAtt); 131 } 132 AppendChild(xDoc, ln, paras); 133 root.AppendChild(ln); 134 try 135 { 136 xDoc.Save(fileFullName); 137 } 138 catch 139 { 140 throw; 141 } 142 } 143 /// <summary> 144 /// 创建一个XML文档 145 /// </summary> 146 /// <param name="fileFullName">文件名称,包括完整路径</param> 147 /// <param name="rootp">根结点对象</param> 148 /// <param name="elemName">元素节点名称</param> 149 /// <param name="paras">XML参数</param> 150 public static void CreateXMLFile(string fileFullName, XmlParameter rootp, string elemName, params XmlParameter[] paras) 151 { 152 XmlDocument xDoc = new XmlDocument(); 153 XmlNode xn; 154 xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); 155 xDoc.AppendChild(xn); 156 XmlNode root = xDoc.CreateElement(rootp.Name); 157 string ns = rootp.NamespaceOfPrefix == null ? "" : root.GetNamespaceOfPrefix(rootp.NamespaceOfPrefix); 158 foreach (AttributeParameter ap in rootp.Attributes) 159 { 160 XmlNode rootAtt = xDoc.CreateNode(XmlNodeType.Attribute, ap.Name, ns == "" ? null : ns); 161 rootAtt.Value = ap.Value; 162 root.Attributes.SetNamedItem(rootAtt); 163 } 164 xDoc.AppendChild(root); 165 XmlNode ln = xDoc.CreateNode(XmlNodeType.Element, elemName, null); 166 AppendChild(xDoc, ln, paras); 167 root.AppendChild(ln); 168 try 169 { 170 xDoc.Save(fileFullName); 171 } 172 catch 173 { 174 throw; 175 } 176 } 177 /// <summary> 178 /// 创建一个XML文档 179 /// </summary> 180 /// <param name="fileFullName">文件名称,包括完整路径</param> 181 /// <param name="rootp">根结点对象</param> 182 /// <param name="elemp">元素节点对象</param> 183 /// <param name="paras">XML参数</param> 184 public static void CreateXMLFile(string fileFullName, XmlParameter rootp, XmlParameter elemp, params XmlParameter[] paras) 185 { 186 XmlDocument xDoc = new XmlDocument(); 187 XmlNode xn; 188 xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); 189 xDoc.AppendChild(xn); 190 XmlNode root = xDoc.CreateElement(rootp.Name); 191 string ns = rootp.NamespaceOfPrefix == null ? "" : root.GetNamespaceOfPrefix(rootp.NamespaceOfPrefix); 192 foreach (AttributeParameter ap in rootp.Attributes) 193 { 194 XmlNode rootAtt = xDoc.CreateNode(XmlNodeType.Attribute, ap.Name, ns == "" ? null : ns); 195 rootAtt.Value = ap.Value; 196 root.Attributes.SetNamedItem(rootAtt); 197 } 198 xDoc.AppendChild(root); 199 XmlNode ln = xDoc.CreateNode(XmlNodeType.Element, elemp.Name, null); 200 ns = elemp.NamespaceOfPrefix == null ? "" : ln.GetNamespaceOfPrefix(elemp.NamespaceOfPrefix); 201 foreach (AttributeParameter ap in elemp.Attributes) 202 { 203 XmlNode elemAtt = xDoc.CreateNode(XmlNodeType.Attribute, ap.Name, ns == "" ? null : ns); 204 elemAtt.Value = ap.Value; 205 ln.Attributes.SetNamedItem(elemAtt); 206 } 207 AppendChild(xDoc, ln, paras); 208 root.AppendChild(ln); 209 try 210 { 211 xDoc.Save(fileFullName); 212 } 213 catch 214 { 215 throw; 216 } 217 } 218 #endregion 219 220 #region AddNewNode 221 /// <summary> 222 /// 添加新节点 223 /// </summary> 224 /// <param name="fileFullName">文件名称,包括完整路径</param> 225 /// <param name="parentNode">新节点的父节点对象</param> 226 /// <param name="paras">XML参数对象</param> 227 public static bool AddNewNode(string fileFullName, XmlNode parentNode, params XmlParameter[] paras) 228 { 229 XmlDocument xDoc = xmlDoc(fileFullName); 230 if (parentNode.Name == xDoc.DocumentElement.Name) 231 { 232 XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes[0].Name, null); 233 AppendChild(xDoc, newNode, paras); 234 xDoc.DocumentElement.AppendChild(newNode); 235 } 236 else 237 { 238 AddEveryNode(xDoc, parentNode, paras); 239 } 240 xDoc.Save(fileFullName); 241 return true; 242 } 243 /// <summary> 244 /// 添加新节点 245 /// </summary> 246 /// <param name="xDoc">XmlDocument对象</param> 247 /// <param name="parentNode">新节点的父节点对象</param> 248 /// <param name="paras">XML参数对象</param> 249 public static bool AddNewNode(XmlDocument xDoc, XmlNode parentNode, params XmlParameter[] paras) 250 { 251 if (parentNode.Name == xDoc.DocumentElement.Name) 252 { 253 XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes[0].Name, null); 254 AppendChild(xDoc, newNode, paras); 255 xDoc.DocumentElement.AppendChild(newNode); 256 } 257 else 258 { 259 AddEveryNode(xDoc, parentNode, paras); 260 } 261 return true; 262 } 263 /// <summary> 264 /// 添加新节点 265 /// </summary> 266 /// <param name="xDoc">XmlDocument对象</param> 267 /// <param name="parentName">新节点的父节点名称</param> 268 /// <param name="paras">XML参数对象</param> 269 public static bool AddNewNode(XmlDocument xDoc, string parentName, params XmlParameter[] paras) 270 { 271 XmlNode parentNode = GetNode(xDoc, parentName); 272 if (parentNode == null) return false; 273 if (parentNode.Name == xDoc.DocumentElement.Name) 274 { 275 XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes[0].Name, null); 276 AppendChild(xDoc, newNode, paras); 277 xDoc.DocumentElement.AppendChild(newNode); 278 } 279 else 280 { 281 AddEveryNode(xDoc, parentNode, paras); 282 } 283 return true; 284 } 285 /// <summary> 286 /// 添加新节点 287 /// </summary> 288 /// <param name="fileFullName">文件名称,包括完整路径</param> 289 /// <param name="parentName">新节点的父节点名称</param> 290 /// <param name="paras">XML参数对象</param> 291 public static bool AddNewNode(string fileFullName, string parentName, params XmlParameter[] paras) 292 { 293 XmlDocument xDoc = xmlDoc(fileFullName); 294 XmlNode parentNode = GetNode(xDoc, parentName); 295 if (parentNode == null) return false; 296 if (parentNode.Name == xDoc.DocumentElement.Name) 297 { 298 XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes[0].Name, null); 299 AppendChild(xDoc, newNode, paras); 300 xDoc.DocumentElement.AppendChild(newNode); 301 } 302 else 303 { 304 AddEveryNode(xDoc, parentNode, paras); 305 } 306 xDoc.Save(fileFullName); 307 return true; 308 } 309 #endregion 310 311 #region AddAttribute 312 /// <summary> 313 /// 添加节点属性 314 /// </summary> 315 /// <param name="xDoc">XmlDocument对象</param> 316 /// <param name="node">节点对象</param> 317 /// <param name="namespaceOfPrefix">该节点的命名空间URI</param> 318 /// <param name="attributeName">新属性名称</param> 319 /// <param name="attributeValue">属性值</param> 320 public static void AddAttribute(XmlDocument xDoc, XmlNode node, string namespaceOfPrefix, string attributeName, string attributeValue) 321 { 322 string ns = namespaceOfPrefix == null ? null : node.GetNamespaceOfPrefix(namespaceOfPrefix); 323 XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attributeName, ns == "" ? null : ns); 324 xn.Value = attributeValue; 325 node.Attributes.SetNamedItem(xn); 326 } 327 /// <summary> 328 /// 添加节点属性 329 /// </summary> 330 /// <param name="xDoc">XmlDocument对象</param> 331 /// <param name="node">节点对象</param> 332 /// <param name="namespaceOfPrefix">该节点的命名空间URI</param> 333 /// <param name="attps">节点属性参数</param> 334 public static void AddAttribute(XmlDocument xDoc, XmlNode node, string namespaceOfPrefix, params AttributeParameter[] attps) 335 { 336 string ns = namespaceOfPrefix == null ? null : node.GetNamespaceOfPrefix(namespaceOfPrefix); 337 foreach (AttributeParameter attp in attps) 338 { 339 XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attp.Name, ns == "" ? null : ns); 340 xn.Value = attp.Value; 341 node.Attributes.SetNamedItem(xn); 342 } 343 } 344 /// <summary> 345 /// 添加节点属性 346 /// </summary> 347 /// <param name="fileFullName">文件名称,包括完整路径</param> 348 /// <param name="node">节点对象</param> 349 /// <param name="namespaceOfPrefix">该节点的命名空间URI</param> 350 /// <param name="attributeName">新属性名称</param> 351 /// <param name="attributeValue">属性值</param> 352 public static void AddAttribute(string fileFullName, XmlNode node, string namespaceOfPrefix, string attributeName, string attributeValue) 353 { 354 XmlDocument xDoc = xmlDoc(fileFullName); 355 AddAttribute(xDoc, node, namespaceOfPrefix, attributeName, attributeValue); 356 xDoc.Save(fileFullName); 357 } 358 /// <summary> 359 /// 添加节点属性 360 /// </summary> 361 /// <param name="fileFullName">文件名称,包括完整路径</param> 362 /// <param name="node">节点对象</param> 363 /// <param name="namespaceOfPrefix">该节点的命名空间URI</param> 364 /// <param name="attps">节点属性参数</param> 365 public static void AddAttribute(string fileFullName, XmlNode node, string namespaceOfPrefix, params AttributeParameter[] attps) 366 { 367 XmlDocument xDoc = xmlDoc(fileFullName); 368 AddAttribute(xDoc, node, namespaceOfPrefix, attps); 369 xDoc.Save(fileFullName); 370 } 371 /// <summary> 372 /// 添加节点属性 373 /// </summary> 374 /// <param name="xDoc">XmlDocument对象</param> 375 /// <param name="nodeName">节点名称</param> 376 /// <param name="namespaceOfPrefix">该节点的命名空间URI</param> 377 /// <param name="attributeName">新属性名称</param> 378 /// <param name="attributeValue">属性值</param> 379 public static void AddAttribute(XmlDocument xDoc, string nodeName, string namespaceOfPrefix, string attributeName, string attributeValue) 380 { 381 XmlNodeList xlst = xDoc.DocumentElement.ChildNodes; 382 for (int i = 0; i < xlst.Count; i++) 383 { 384 XmlNode node = GetNode(xlst[i], nodeName); 385 if (node == null) return; 386 string ns = namespaceOfPrefix == null ? null : node.GetNamespaceOfPrefix(namespaceOfPrefix); 387 XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attributeName, ns == "" ? null : ns); 388 xn.Value = attributeValue; 389 node.Attributes.SetNamedItem(xn); 390 } 391 } 392 /// <summary> 393 /// 添加节点属性 394 /// </summary> 395 /// <param name="xDoc">XmlDocument对象</param> 396 /// <param name="nodeName">节点名称</param> 397 /// <param name="namespaceOfPrefix">该节点的命名空间URI</param> 398 /// <param name="attps">节点属性参数</param> 399 public static void AddAttribute(XmlDocument xDoc, string nodeName, string namespaceOfPrefix, params AttributeParameter[] attps) 400 { 401 XmlNodeList xlst = xDoc.DocumentElement.ChildNodes; 402 for (int i = 0; i < xlst.Count; i++) 403 { 404 XmlNode node = GetNode(xlst[i], nodeName); 405 if (node == null) return; 406 string ns = namespaceOfPrefix == null ? null : node.GetNamespaceOfPrefix(namespaceOfPrefix); 407 foreach (AttributeParameter attp in attps) 408 { 409 XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attp.Name, ns == "" ? null : ns); 410 xn.Value = attp.Value; 411 node.Attributes.SetNamedItem(xn); 412 } 413 } 414 } 415 /// <summary> 416 /// 添加节点属性 417 /// </summary> 418 /// <param name="fileFullName">文件名称,包括完整路径</param> 419 /// <param name="nodeName">节点名称</param> 420 /// <param name="namespaceOfPrefix">该节点的命名空间URI</param> 421 /// <param name="attributeName">新属性名称</param> 422 /// <param name="attributeValue">属性值</param> 423 public static void AddAttribute(string fileFullName, string nodeName, string namespaceOfPrefix, string attributeName, string attributeValue) 424 { 425 XmlDocument xDoc = xmlDoc(fileFullName); 426 XmlNodeList xlst = xDoc.DocumentElement.ChildNodes; 427 for (int i = 0; i < xlst.Count; i++) 428 { 429 XmlNode node = GetNode(xlst[i], nodeName); 430 if (node == null) break; 431 AddAttribute(xDoc, node, namespaceOfPrefix, attributeName, attributeValue); 432 } 433 xDoc.Save(fileFullName); 434 } 435 /// <summary> 436 /// 添加节点属性 437 /// </summary> 438 /// <param name="fileFullName">文件名称,包括完整路径</param> 439 /// <param name="nodeName">节点名称</param> 440 /// <param name="namespaceOfPrefix">该节点的命名空间URI</param> 441 /// <param name="attps">节点属性参数</param> 442 public static void AddAttribute(string fileFullName, string nodeName, string namespaceOfPrefix, params AttributeParameter[] attps) 443 { 444 XmlDocument xDoc = xmlDoc(fileFullName); 445 XmlNodeList xlst = xDoc.DocumentElement.ChildNodes; 446 for (int i = 0; i < xlst.Count; i++) 447 { 448 XmlNode node = GetNode(xlst[i], nodeName); 449 if (node == null) break; 450 AddAttribute(xDoc, node, namespaceOfPrefix, attps); 451 } 452 xDoc.Save(fileFullName); 453 } 454 #endregion 455 456 #region GetNode 457 /// <summary> 458 /// 获取指定节点名称的节点对象 459 /// </summary> 460 /// <param name="fileFullName">文件名称,包括完整路径</param> 461 /// <param name="nodeName">节点名称</param> 462 /// <returns></returns> 463 public static XmlNode GetNode(string fileFullName, string nodeName) 464 { 465 XmlDocument xDoc = xmlDoc(fileFullName); 466 if (xDoc.DocumentElement.Name == nodeName) return (XmlNode)xDoc.DocumentElement; 467 XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; 468 foreach (XmlNode xns in nlst) // 遍历所有子节点 469 { 470 if (xns.Name.ToLower() == nodeName.ToLower()) return xns; 471 else 472 { 473 XmlNode xn = GetNode(xns, nodeName); 474 if (xn != null) return xn; /// V1.0.0.3添加节点判断 475 } 476 } 477 return null; 478 } 479 /// <summary> 480 /// 获取指定节点名称的节点对象 481 /// </summary> 482 /// <param name="node">节点对象</param> 483 /// <param name="nodeName">节点名称</param> 484 /// <returns></returns> 485 public static XmlNode GetNode(XmlNode node, string nodeName) 486 { 487 foreach (XmlNode xn in node) 488 { 489 if (xn.Name.ToLower() == nodeName.ToLower()) return xn; 490 else 491 { 492 XmlNode tmp = GetNode(xn, nodeName); 493 if (tmp != null) return tmp; 494 } 495 } 496 return null; 497 } 498 /// <summary> 499 /// 获取指定节点名称的节点对象 500 /// </summary> 501 /// <param name="xDoc">XmlDocument对象</param> 502 /// <param name="nodeName">节点名称</param> 503 /// <returns></returns> 504 public static XmlNode GetNode(XmlDocument xDoc, string nodeName) 505 { 506 if (xDoc.DocumentElement.Name == nodeName) return (XmlNode)xDoc.DocumentElement; 507 XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; 508 foreach (XmlNode xns in nlst) // 遍历所有子节点 509 { 510 if (xns.Name.ToLower() == nodeName.ToLower()) return xns; 511 else 512 { 513 XmlNode xn = GetNode(xns, nodeName); 514 if (xn != null) return xn; /// 添加节点判断, 避免只查询一个节点 515 } 516 } 517 return null; 518 } 519 /// <summary> 520 /// 获取指定节点名称的节点对象 521 /// </summary> 522 /// <param name="xDoc">XmlDocument对象</param> 523 /// <param name="Index">节点索引</param> 524 /// <param name="nodeName">节点名称</param> 525 public static XmlNode GetNode(XmlDocument xDoc, int Index, string nodeName) 526 { 527 XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; 528 if (nlst.Count <= Index) return null; 529 if (nlst[Index].Name.ToLower() == nodeName.ToLower()) return (XmlNode)nlst[Index]; 530 foreach (XmlNode xn in nlst[Index]) 531 { 532 return GetNode(xn, nodeName); 533 } 534 return null; 535 } 536 /// <summary> 537 /// 获取指定节点名称的节点对象 538 /// </summary> 539 /// <param name="fileFullName">文件名称,包括完整路径</param> 540 /// <param name="Index">节点索引</param> 541 /// <param name="nodeName">节点名称</param> 542 public static XmlNode GetNode(string fileFullName, int Index, string nodeName) 543 { 544 XmlDocument xDoc = xmlDoc(fileFullName); 545 XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; 546 if (nlst.Count <= Index) return null; 547 if (nlst[Index].Name.ToLower() == nodeName.ToLower()) return (XmlNode)nlst[Index]; 548 foreach (XmlNode xn in nlst[Index]) 549 { 550 return GetNode(xn, nodeName); 551 } 552 return null; 553 } 554 /// <summary> 555 /// 获取指定节点名称的节点对象 556 /// </summary> 557 /// <param name="node">节点对象</param> 558 /// <param name="nodeName">节点名称</param> 559 /// <param name="innerText">节点内容</param> 560 public static XmlNode GetNode(XmlNode node, string nodeName, string innerText) 561 { 562 foreach (XmlNode xn in node) 563 { 564 if (xn.Name.ToLower() == nodeName.ToLower() && xn.InnerText == innerText) return xn; 565 else 566 { 567 XmlNode tmp = GetNode(xn, nodeName, innerText); 568 if (tmp != null) return tmp; 569 } 570 } 571 return null; 572 } 573 /// <summary> 574 /// 获取指定节点名称的节点对象 575 /// </summary> 576 /// <param name="xDoc">XmlDocument对象</param> 577 /// <param name="nodeName">节点名称</param> 578 /// <param name="innerText">节点内容</param> 579 public static XmlNode GetNode(XmlDocument xDoc, string nodeName, string innerText) 580 { 581 XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; 582 foreach (XmlNode xns in nlst) // 遍历所有子节点 583 { 584 if (xns.Name.ToLower() == nodeName.ToLower() && xns.InnerText == innerText) return xns; 585 XmlNode tmp = GetNode(xns, nodeName, innerText); 586 if (tmp != null) return tmp; 587 } 588 return null; 589 } 590 /// <summary> 591 /// 获取指定节点名称的节点对象 592 /// </summary> 593 /// <param name="xDoc">XmlDocument对象</param> 594 /// <param name="xpar">XML参数</param> 595 public static XmlNode GetNode(XmlDocument xDoc, XmlParameter xpar) 596 { 597 return GetNode(xDoc, xpar.Name, xpar.InnerText); 598 } 599 /// <summary> 600 /// 获取指定节点名称的节点对象 601 /// </summary> 602 /// <param name="node">节点对象</param> 603 /// <param name="xpar">XML参数</param> 604 public static XmlNode GetNode(XmlNode node, XmlParameter xpar) 605 { 606 return GetNode(node, xpar.Name, node.InnerText); 607 } 608 #endregion 609 610 #region UpdateNode 611 /// <summary> 612 /// 修改节点的内容 613 /// </summary> 614 /// <param name="node">修改的节点对象</param> 615 /// <param name="para">XML参数对象</param> 616 public static void UpdateNode(XmlNode node, XmlParameter para) 617 { 618 node.InnerText = para.InnerText; 619 for (int i = 0; i < para.Attributes.Length; i++) 620 { 621 node.Attributes.Item(i).Value = para.Attributes[i].Value; 622 } 623 } 624 /// <summary> 625 /// 修改节点的内容 626 /// </summary> 627 /// <param name="node">父节点对象</param> 628 /// <param name="childIndex">该节点的索引</param> 629 /// <param name="nodeText">修改后的内容</param> 630 public static void UpdateNode(XmlNode node, int childIndex, string nodeText) 631 { 632 node.ChildNodes[childIndex].InnerText = nodeText; 633 } 634 /// <summary> 635 /// 修改节点的内容 636 /// </summary> 637 /// <param name="node">修改的节点对象</param> 638 /// <param name="nodeText">修改后的内容</param> 639 public static void UpdateNode(XmlNode node, string nodeText) 640 { 641 node.InnerText = nodeText; 642 } 643 /// <summary> 644 /// 修改节点的内容 645 /// </summary> 646 /// <param name="xDoc">XMLDocument对象</param> 647 /// <param name="para">XML参数对象</param> 648 public static void UpdateNode(XmlDocument xDoc, int Index, XmlParameter para) 649 { 650 XmlNode node = GetNode(xDoc, Index, para.Name); 651 UpdateNode(node, para); 652 } 653 /// <summary> 654 /// 修改节点的内容 655 /// </summary> 656 /// <param name="xDoc">XMLDocument对象</param> 657 /// <param name="nodeName">父节点名称</param> 658 /// <param name="childIndex">该节点的索引</param> 659 /// <param name="nodeText">修改后的内容</param> 660 /// <param name="nodeValue">修改后的值,如果没有,那么该值为null</param> 661 public static void UpdateNode(XmlDocument xDoc, int Index, string nodeName, int childIndex, string nodeText) 662 { 663 XmlNode node = GetNode(xDoc, Index, nodeName); 664 UpdateNode(node, childIndex, nodeText); 665 } 666 /// <summary> 667 /// 修改节点的内容 668 /// </summary> 669 /// <param name="xDoc">XMLDocument对象</param> 670 /// <param name="nodeName">修改的节点名称</param> 671 /// <param name="nodeText">修改后的内容</param> 672 /// <param name="nodeValue">修改后的值,如果没有,那么该值为null</param> 673 public static void UpdateNode(XmlDocument xDoc, int Index, string nodeName, string nodeText) 674 { 675 XmlNode node = GetNode(xDoc, Index, nodeName); 676 UpdateNode(node, nodeText); 677 } 678 /// <summary> 679 /// 修改节点的内容 680 /// </summary> 681 /// <param name="fileFullName">文件名称,包括完整路径</param> 682 /// <param name="para">XML参数对象</param> 683 public static void UpdateNode(string fileFullName, int Index, XmlParameter para) 684 { 685 XmlDocument xDoc = xmlDoc(fileFullName); 686 UpdateNode(xDoc, Index, para); 687 xDoc.Save(fileFullName); 688 } 689 /// <summary> 690 /// 修改节点的内容 691 /// </summary> 692 /// <param name="fileFullName">文件名称,包括完整路径</param> 693 /// <param name="nodeName">父节点名称</param> 694 /// <param name="childIndex">该节点的索引</param> 695 /// <param name="nodeText">修改后的内容</param> 696 public static void UpdateNode(string fileFullName, int Index, string nodeName, int childIndex, string nodeText) 697 { 698 XmlDocument xDoc = xmlDoc(fileFullName); 699 UpdateNode(xDoc, Index, nodeName, childIndex, nodeText); 700 xDoc.Save(fileFullName); 701 } 702 /// <summary> 703 /// 修改节点的内容 704 /// </summary> 705 /// <param name="fileFullName">文件名称,包括完整路径</param> 706 /// <param name="nodeName">修改的节点名称</param> 707 /// <param name="nodeText">修改后的内容</param> 708 public static void UpdateNode(string fileFullName, int Index, string nodeName, string nodeText) 709 { 710 XmlDocument xDoc = xmlDoc(fileFullName); 711 UpdateNode(xDoc, Index, nodeName, nodeText); 712 xDoc.Save(fileFullName); 713 } 714 #endregion 715 716 #region DeleteNode 717 /// <summary> 718 /// 删除节点 719 /// </summary> 720 /// <param name="xDoc">XmlDocument对象</param> 721 /// <param name="Index">节点索引</param> 722 public static void DeleteNode(XmlDocument xDoc, int Index) 723 { 724 XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; 725 nlst[Index].ParentNode.RemoveChild(nlst[Index]); 726 } 727 /// <summary> 728 /// 删除节点 729 /// </summary> 730 /// <param name="fileFullName">文件名称,包括完整路径</param> 731 /// <param name="Index">节点索引</param> 732 public static void DeleteNode(string fileFullName, int Index) 733 { 734 XmlDocument xDoc = xmlDoc(fileFullName); 735 DeleteNode(xDoc, Index); 736 xDoc.Save(fileFullName); 737 } 738 /// <summary> 739 /// 删除节点 740 /// </summary> 741 /// <param name="xDoc">XmlDocument对象</param> 742 /// <param name="xns">需要删除的节点对象</param> 743 public static void DeleteNode(XmlDocument xDoc, params XmlNode[] xns) 744 { 745 foreach (XmlNode xnl in xns) 746 { 747 foreach (XmlNode xn in xDoc.DocumentElement.ChildNodes) 748 { 749 if (xnl.Equals(xn)) 750 { 751 xn.ParentNode.RemoveChild(xn); 752 break; 753 } 754 } 755 } 756 } 757 /// <summary> 758 /// 删除节点 759 /// </summary> 760 /// <param name="fileFullName">文件名称,包括完整路径</param> 761 /// <param name="xns">需要删除的节点对象</param> 762 public static void DeleteNode(string fileFullName, params XmlNode[] xns) 763 { 764 XmlDocument xDoc = xmlDoc(fileFullName); 765 DeleteNode(xDoc, xns); 766 xDoc.Save(fileFullName); 767 } 768 /// <summary> 769 /// 删除节点 770 /// </summary> 771 /// <param name="xDoc">XmlDocument对象</param> 772 /// <param name="nodeName">节点名称</param> 773 /// <param name="nodeText">节点内容</param> 774 public static void DeleteNode(XmlDocument xDoc, string nodeName, string nodeText) 775 { 776 foreach (XmlNode xn in xDoc.DocumentElement.ChildNodes) 777 { 778 if (xn.Name == nodeName) 779 { 780 if (xn.InnerText == nodeText) 781 { 782 xn.ParentNode.RemoveChild(xn); 783 return; 784 } 785 } 786 else 787 { 788 XmlNode node = GetNode(xn, nodeName); 789 if (node != null && node.InnerText == nodeText) 790 { 791 node.ParentNode.ParentNode.RemoveChild(node.ParentNode); 792 return; 793 } 794 } 795 } 796 } 797 /// <summary> 798 /// 删除节点 799 /// </summary> 800 /// <param name="fileFullName">文件名称,包括完整路径</param> 801 /// <param name="nodeName">节点名称</param> 802 /// <param name="nodeText">节点内容</param> 803 public static void DeleteNode(string fileFullName, string nodeName, string nodeText) 804 { 805 XmlDocument xDoc = xmlDoc(fileFullName); 806 DeleteNode(xDoc, nodeName, nodeText); 807 xDoc.Save(fileFullName); 808 } 809 #endregion 810 811 #region SetAttribute 812 /// <summary> 813 /// 修改属性值 814 /// </summary> 815 /// <param name="node">节点对象</param> 816 /// <param name="attps">属性参数</param> 817 public static void SetAttribute(XmlNode node, params AttributeParameter[] attps) 818 { 819 XmlElement xe = (XmlElement)node; 820 foreach (AttributeParameter attp in attps) 821 { 822 xe.SetAttribute(attp.Name, attp.Value); 823 } 824 } 825 /// <summary> 826 /// 修改属性值 827 /// </summary> 828 /// <param name="node">节点对象</param> 829 /// <param name="attributeName">属性名称</param> 830 /// <param name="attributeValue">属性值</param> 831 public static void SetAttribute(XmlNode node, string attributeName, string attributeValue) 832 { 833 XmlElement xe = (XmlElement)node; 834 xe.SetAttribute(attributeName, attributeValue); 835 } 836 /// <summary> 837 /// 修改属性值 838 /// </summary> 839 /// <param name="elem">元素对象</param> 840 /// <param name="attps">属性参数</param> 841 public static void SetAttribute(XmlElement elem, params AttributeParameter[] attps) 842 { 843 foreach (AttributeParameter attp in attps) 844 { 845 elem.SetAttribute(attp.Name, attp.Value); 846 } 847 } 848 /// <summary> 849 /// 修改属性值 850 /// </summary> 851 /// <param name="elem">元素对象</param> 852 /// <param name="attributeName">属性名称</param> 853 /// <param name="attributeValue">属性值</param> 854 public static void SetAttribute(XmlElement elem, string attributeName, string attributeValue) 855 { 856 elem.SetAttribute(attributeName, attributeValue); 857 } 858 /// <summary> 859 /// 修改属性值 860 /// </summary> 861 /// <param name="xDoc">XmlDocument对象</param> 862 /// <param name="xpara">XML参数</param> 863 /// <param name="attps">属性参数</param> 864 public static void SetAttribute(XmlDocument xDoc, XmlParameter xpara, params AttributeParameter[] attps) 865 { 866 XmlElement xe = (XmlElement)GetNode(xDoc, xpara); 867 if (xe == null) return; 868 SetAttribute(xe, attps); 869 } 870 /// <summary> 871 /// 修改属性值 872 /// </summary> 873 /// <param name="xDoc">XmlDocument对象</param> 874 /// <param name="xpara">XML参数</param> 875 /// <param name="newValue">新属性值</param> 876 public static void SetAttribute(XmlDocument xDoc, XmlParameter xpara, string attributeName, string newValue) 877 { 878 XmlElement xe = (XmlElement)GetNode(xDoc, xpara); 879 if (xe == null) return; 880 SetAttribute(xe, attributeName, newValue); 881 } 882 /// <summary> 883 /// 修改属性值 884 /// </summary> 885 /// <param name="fileFullName">文件名称,包括完整路径</param> 886 /// <param name="xpara">XML参数</param> 887 /// <param name="newValue">新属性值</param> 888 public static void SetAttribute(string fileFullName, XmlParameter xpara, string attributeName, string newValue) 889 { 890 XmlDocument xDoc = xmlDoc(fileFullName); 891 SetAttribute(xDoc, xpara, attributeName, newValue); 892 xDoc.Save(fileFullName); 893 } 894 /// <summary> 895 /// 修改属性值 896 /// </summary> 897 /// <param name="fileFullName">文件名称,包括完整路径</param> 898 /// <param name="xpara">XML参数</param> 899 /// <param name="attps">属性参数</param> 900 public static void SetAttribute(string fileFullName, XmlParameter xpara, params AttributeParameter[] attps) 901 { 902 XmlDocument xDoc = xmlDoc(fileFullName); 903 SetAttribute(xDoc, xpara, attps); 904 xDoc.Save(fileFullName); 905 } 906 #endregion 907 } 908 909 public sealed class XmlParameter 910 { 911 private string name; 912 private string innerText; 913 private string namespaceOfPrefix; 914 private AttributeParameter[] attributes; 915 916 public XmlParameter() 917 { 918 // 919 // TODO: Add constructor logic here 920 // 921 this.namespaceOfPrefix = null; 922 } 923 924 public XmlParameter(string name, params AttributeParameter[] attParas) 925 { 926 this.name = name; 927 this.namespaceOfPrefix = null; 928 this.attributes = attParas; 929 } 930 931 932 public XmlParameter(string name, string innerText, params AttributeParameter[] attParas) 933 { 934 this.name = name; 935 this.innerText = innerText; 936 this.namespaceOfPrefix = null; 937 this.attributes = attParas; 938 } 939 940 public XmlParameter(string name, string innerText, string namespaceOfPrefix, params AttributeParameter[] attParas) 941 { 942 this.name = name; 943 this.innerText = innerText; 944 this.namespaceOfPrefix = namespaceOfPrefix; 945 this.attributes = attParas; 946 } 947 948 public string Name 949 { 950 get { return this.name; } 951 set { this.name = value; } 952 } 953 954 public string InnerText 955 { 956 get { return this.innerText; } 957 set { this.innerText = value; } 958 } 959 960 public string NamespaceOfPrefix 961 { 962 get { return this.namespaceOfPrefix; } 963 set { this.namespaceOfPrefix = value; } 964 } 965 966 public AttributeParameter[] Attributes 967 { 968 get { return this.attributes; } 969 set { this.attributes = value; } 970 } 971 } 972 973 public sealed class AttributeParameter 974 { 975 private string name; 976 private string value; 977 978 public AttributeParameter() 979 { 980 // 981 // TODO: Add constructor logic here 982 // 983 } 984 public AttributeParameter(string attributeName, string attributeValue) 985 { 986 this.name = attributeName; 987 this.value = attributeValue; 988 } 989 public string Name 990 { 991 get { return this.name; } 992 set { this.name = value; } 993 } 994 public string Value 995 { 996 get { return this.value; } 997 set { this.value = value; } 998 } 999 } 1000 1001 }