237 try
238 {
239 //可以批量为符合条件的节点的属性付值
240 XmlNodeList xmlNode=this.xmlDoc.SelectNodes(xmlNodePath);
241 if (!(xmlNode==null))
242 {
243 foreach(XmlNode xn in xmlNode)
244 {
245 XmlAttributeCollection xmlAttr = xn.Attributes ;
246 for(int i=0 ; i<xmlAttr.Count ; i++)
247 {
248 if ( xmlAttr.Item(i).Name == xmlNodeAttribute )
249 {
250 xmlAttr.Item(i).Value = xmlNodeAttributeValue;
251 break ;
252 }
253 }
254 }
255 }
[NextPage
256 /**//*单个节点
257 //根据指定路径获取节点
258 XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;
259 if (!(xmlNode==null))
260 {//获取节点的属性,并循环取出需要的属性值
261 XmlAttributeCollection xmlAttr = xmlNode.Attributes ;
262 for(int i=0 ; i<xmlAttr.Count ; i++)
263 {
264 if ( xmlAttr.Item(i).Name == xmlNodeAttribute )
265 {
266 xmlAttr.Item(i).Value = xmlNodeAttributeValue;
267 break ;
268 }
269 }
270 }
271 */
272 }
273 catch(XmlException xmle)
274 {
275 throw xmle ;
276 }
277 }
278 #endregion
279
280 添加#region 添加
281 /** <summary>
282 /// 获取XML文件的根元素
283 /// </summary>
284 public XmlNode GetXmlRoot()
285 {
286 return xmlDoc.DocumentElement ;
287 }
288
289 /** <summary>
290 /// 在根节点下添加父节点
291 /// </summary>
292 public void AddParentNode(string parentNode)
293 {
294 try
295 {
296 XmlNode root = GetXmlRoot() ;
297 XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode) ;
298 root.AppendChild(parentXmlNode) ;
299 }
300 catch(XmlException xmle)
301 {
302 throw xmle ;
303 }
304 }
305
306 /** <summary>
307 /// 向一个已经存在的父节点中插入一个子节点
308 /// </summary>
309 /// <param name="parentNodePath">父节点</param>
310 /// <param name="childNodePath">字节点名称</param>
311 public void AddChildNode( string parentNodePath,string childnodename )
312 {
313 try
314 {
315 XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath) ;
316 if(!((parentXmlNode)==null))//如果此节点存在
317 {
318 XmlNode childXmlNode = xmlDoc.CreateElement(childnodename) ;
319 parentXmlNode.AppendChild( childXmlNode ) ;
320 }
321 else{//如果不存在就放父节点添加
322 //this.GetXmlRoot().AppendChild(childXmlNode);
323 }
324