ÎÂܰÌáʾ£º´úÂëÔÚÏßä¯ÀÀ¹¦ÄÜÖ»ÄÜ×öΪԴÂëä¯ÀÀ²Î¿¼£¬²»ÄÜչʾÏîÄ¿µÄÈ«²¿£¬Èç¹ûÏë¸ü½øÒ»²½Á˽â¸Ã´úÂëÇëÏÂÔØ£ºXMLͬѧ¼ϵͳԴÂ루±ÏÒµÉè¼Æ£©
µ±Ç°Îļþ·¾¶£ºXMLClassBooks/App_Code/XmlOp.cs

1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Web.UI.WebControls.WebParts; 9
using System.Web.UI.HtmlControls; 10
using System.Collections; 11
12
//ÒýÓÃÃüÃû¿Õ¼ä 13
using System.Xml; 14
15
16
namespace xmlOp 17
{ 18
/// <summary> 19
/// XmlOpÀàÌṩ¶ÔXMLÊý¾Ý¿âµÄ¶Áд 20
/// </summary> 21
public class XmlOp 22
{ 23
//ÉùÃ÷Ò»¸öXmlDocument¿Õ¶ÔÏó 24
protected XmlDocument XmlDoc = new XmlDocument(); 25
26
/// <summary> 27
/// ¹¹Ô캯Êý£¬µ¼ÈëXmlÎļþ 28
/// </summary> 29
/// <param name="xmlFile">ÎļþÐéÄâ·¾¶</param> 30
public XmlOp(string xmlFile) 31
{ 32
try 33
{ 34
XmlDoc.Load(GetXmlFilePath(xmlFile)); //ÔØÈëXmlÎĵµ 35
} 36
catch (System.Exception ex) 37
{ 38
throw ex; 39
} 40
} 41
/// <summary> 42
/// Îö¹¹º¯Êý 43
/// </summary> 44
~XmlOp() 45
{ 46
XmlDoc = null; //ÊÍ·ÅXmlDocument¶ÔÏó 47
} 48
49
/// <summary> 50
/// ±£´æÎļþ 51
/// </summary> 52
/// <param name="filePath">ÎļþÐéÄâ·¾¶</param> 53
public void Save(string filePath) 54
{ 55
try 56
{ 57
XmlDoc.Save(GetXmlFilePath(filePath)); 58
} 59
catch (System.Exception ex) 60
{ 61
throw ex; 62
} 63
} 64
65
/// <summary> 66
/// ·µ»ØXmlÎļþʵ¼Ê·¾¶ 67
/// </summary> 68
/// <param name="xmlFile">ÎļþÐéÄâ·¾¶</param> 69
/// <returns></returns> 70
public string GetXmlFilePath(string xmlFile) 71
{ 72
return HttpContext.Current.Server.MapPath(xmlFile); 73
} 74
75
/// <summary> 76
/// ¸ù¾ÝXmlÎļþµÄ½Úµã·¾¶£¬·µ»ØÒ»¸öDataSetÊý¾Ý¼¯ 77
/// </summary> 78
/// <param name="XmlPathNode">XmlÎļþµÄij¸ö½Úµã</param> 79
/// <returns></returns> 80
public DataSet GetDs(string XmlPathNode) 81
{ 82
DataSet ds = new DataSet(); 83
try 84
{ 85
System.IO.StringReader read = new System.IO.StringReader(XmlDoc.SelectSingleNode(XmlPathNode).OuterXml); 86
ds.ReadXml(read); //ÀûÓÃDataSetµÄReadXml·½·¨¶ÁÈ¡StringReaderÎļþÁ÷ 87
read.Close(); 88
} 89
catch 90
{ } 91
return ds; 92
} 93
94
/// <summary> 95
/// ÊôÐÔ²éѯ£¬·µ»ØÊôÐÔÖµ 96
/// </summary> 97
/// <param name="XmlPathNode">ÊôÐÔËùÔڵĽڵã</param> 98
/// <param name="Attrib">ÊôÐÔ</param> 99
/// <returns></returns> 100
public string SelectAttrib(string XmlPathNode, string Attrib) 101
{ 102
string _strNode=""; 103
try 104
{ 105
_strNode = XmlDoc.SelectSingleNode(XmlPathNode).Attributes[Attrib].Value; 106
} 107
catch 108
{ } 109
return _strNode; 110
} 111
112
/// <summary> 113
/// ½Úµã²éѯ£¬·µ»Ø½ÚµãÖµ 114
/// </summary> 115
/// <param name="XmlPathNode">½ÚµãµÄ·¾¶</param> 116
/// <returns></returns> 117
public string SelectNodeText(string XmlPathNode) 118
{ 119
string _nodeTxt = XmlDoc.SelectSingleNode(XmlPathNode).InnerText; 120
if (_nodeTxt == null || _nodeTxt == "") 121
return ""; 122
else 123
return _nodeTxt; 124
} 125
126
/// <summary> 127
/// ½ÚµãÖµ²éѯÅÐ¶Ï 128
/// </summary> 129
/// <param name="XmlPathNode">¸¸½Úµã</param> 130
/// <param name="index">½ÚµãË÷Òý</param> 131
/// <param name="NodeText">½ÚµãÖµ</param> 132
/// <returns></returns> 133
public bool SelectNode(string XmlPathNode, int index, string NodeText) 134
{ 135
try 136
{ 137
XmlNodeList _NodeList = XmlDoc.SelectNodes(XmlPathNode); 138
//Ñ»·±éÀú½Úµã£¬²éѯÊÇ·ñ´æÔڸýڵã 139
for (int i = 0; i < _NodeList.Count; i++) 140
{ 141
if (_NodeList[i].ChildNodes[index].InnerText == NodeText) 142
{ 143
return true; 144
break; 145
} 146
} 147
} 148
catch 149
{ 150
} 151
return false; 152
} 153
154
/// <summary> 155
/// »ñÈ¡×Ó½Úµã¸öÊý 156
/// </summary> 157
/// <param name="XmlPathNode">¸¸½Úµã</param> 158
/// <returns></returns> 159
public int NodeCount(string XmlPathNode) 160
{ 161
int i = 0; 162
try 163
{ 164
i=XmlDoc.SelectSingleNode(XmlPathNode).ChildNodes.Count; 165
} 166
catch 167
{ 168
i=0; 169
} 170
return i; 171
} 172
173
/// <summary> 174
/// ¸üÐÂÒ»¸ö½ÚµãµÄÄÚÈÝ 175
/// </summary> 176
/// <param name="XmlPathNode">½ÚµãµÄ·¾¶</param> 177
/// <param name="Content">еĽڵãÖµ</param> 178
/// <returns></returns> 179
public bool UpdateNode(string XmlPathNode,string NodeContent) 180
{ 181
try 182
{ 183
XmlDoc.SelectSingleNode(XmlPathNode).InnerText = NodeContent; 184
return true; 185
} 186
catch 187
{ 188
return false; 189
} 190
} 191
192
/// <summary> 193
/// ¸üÐÂN¸ö½ÚµãÖµ 194
/// </summary> 195
/// <param name="XmlParentNode">¸¸½Úµã</param> 196
/// <param name="XmlNode">×Ó½Úµã</param> 197
/// <param name="NodeContent">×Ó½ÚµãÄÚÈÝ</param> 198
/// <returns></returns> 199
public bool UpdateNode(string XmlParentNode, string[] XmlNode, string[] NodeContent) 200
{ 201
try 202
{ 203
//¸ù¾Ý½ÚµãÊý×éÑ»·Ð޸ĽڵãÖµ 204
for (int i = 0; i < XmlNode.Length; i++) 205
{ 206
XmlDoc.SelectSingleNode(XmlParentNode+"/"+XmlNode[i]).InnerText = NodeContent[i]; 207
} 208
return true; 209
} 210
catch 211
{ 212
return false; 213
} 214
} 215
216
/// <summary> 217
/// ÐÞ¸ÄÊôÐÔ 218
/// </summary> 219
/// <param name="XmlPathNode">ÊôÐÔËùÔڵĽڵã</param> 220
/// <param name="Attrib">ÊôÐÔÃû</param> 221
/// <param name="Content">ÊôÐÔÖµ</param> 222
/// <returns></returns> 223
public bool UpdateAttrib(string XmlPathNode, string Attrib,string AttribContent) 224
{ 225
try 226
{ 227
//ÐÞ¸ÄÊôÐÔÖµ 228
XmlDoc.SelectSingleNode(XmlPathNode).Attributes[Attrib].Value = AttribContent; 229
return true; 230
} 231
catch 232
{ 233
return false; 234
} 235
} 236
237
238
/// <summary> 239
/// Ìí¼ÓÊôÐÔ 240
/// </summary> 241
/// <param name="MainNode">ÊôÐÔËùÔÚ½Úµã</param> 242
/// <param name="Attrib">ÊôÐÔÃû</param> 243
/// <param name="AttribContent">ÊôÐÔÖµ</param> 244
/// <returns></returns> 245
public bool InsertAttrib(string MainNode,string Attrib, string AttribContent) 246
{ 247
try 248
{ 249
XmlElement objNode = (XmlElement)XmlDoc.SelectSingleNode(MainNode); //Ç¿ÖÆ×ª»¯³ÉXmlElement¶ÔÏó 250
objNode.SetAttribute(Attrib, AttribContent); //XmlElement¶ÔÏóÌí¼ÓÊôÐÔ·½·¨ 251
return true; 252
} 253
catch 254
{ 255
return false; 256
} 257
} 258
259
260
/// <summary> 261
/// ²åÈëÒ»¸ö½Úµã£¬´øN¸ö×Ó½Úµã 262
/// </summary> 263
/// <param name="MainNode">²åÈë½ÚµãµÄ¸¸½Úµã</param> 264
/// <param name="ChildNode">²åÈë½ÚµãµÄÔªËØÃû</param> 265
/// <param name="Element">²åÈë½ÚµãµÄ×Ó½ÚµãÃûÊý×é</param> 266
/// <param name="Content">²åÈë½ÚµãµÄ×Ó½ÚµãÄÚÈÝÊý×é</param> 267
/// <returns></returns> 268
public bool InsertNode(string MainNode, string ChildNode, string[] Element, string[] Content) 269
{ 270
try 271







