首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网页制作-> Javascript教程
.net操纵xml文件类(c#)-.NET教程,C#语言
作者:网友供稿 点击:124
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
 

一直想要写一个操作xml文件的类,今天在网上找了一下,找到一个已写的差不多的类,对其进行扩展与修改,最终成了以下代码,供新手参考参考.
//
在此类中用到了xml事件.此类中对于节点的查找必需用xpath表达式,如果你对xpath表达式不了解可以查看我收藏的另外一篇文章:+xml文件操作:[学习xpath]xpath最通俗的教程+

  1using system;
  2using system.xml;
  3using system.web;
  4namespace solucky
  5{
  6    /**//// <summary>
  7    /// 必需用xpath表达式来获取相应节点
  8    /// 关于xpath可以参见:
  9    /// </summary>
 10    public class myxml
 11    {
 12        变量#region 变量
 13        /**//// <summary>
 14        /// xml文件所在路径类型
 15        /// </summary>
 16        /// <remarks>xml文件所在路径类型</remarks>
 17        public enum enumxmlpathtype
 18        {   
 19            /**//// <summary>
 20            /// 绝对路径
 21            /// </summary>
 22            absolutepath,
 23            /**//// <summary>
 24            /// 虚拟路径
 25            /// </summary>
 26            virtualpath
 27        }
 28
 29        private string xmlfilepath ;
 30        private enumxmlpathtype xmlfilepathtype ;
 31        private xmldocument xmldoc = new xmldocument() ;
 32        #endregion
 33
 34
 35        属性#region 属性
 36        /**//// <summary>
 37        /// 文件路径
 38        /// </summary>
 39        /// <remarks>文件路径</remarks>
 40        public string xmlfilepath
 41        {
 42            get
 43            {
 44                return this.xmlfilepath;
 45            }
 46            set
 47            {
 48                xmlfilepath = value ;
 49
 50            }
 51        }
 52
 53        /**//// <summary>
 54        /// 文件路径类型
 55        /// </summary>
 56        public enumxmlpathtype xmlfilepathtyp
 57        {
 58            set
 59            {
 60                xmlfilepathtype = value ;
 61            }
 62        }
 63        #endregion
 64
 65        构造函数#region 构造函数
 66        /**//// <summary>
 67        ///
 68        /// </summary>
 69        /// <param name="tempxmlfilepath"></param>
 70        public myxml( string tempxmlfilepath )
 71        {
 72            //
 73            // todo: 在此处添加构造函数逻辑
 74            //
 75
 76            this.xmlfilepathtype = enumxmlpathtype.virtualpath ;
 77            this.xmlfilepath = tempxmlfilepath ;
 78            getxmldocument() ;
 79            //xmldoc.load( xmlfilepath ) ;
 80        }
 81   
 82        /**//// <summary>
 83        /// 构造函数
 84        /// </summary>
 85        /// <param name="tempxmlfilepath">文件路径</param>
 86        /// <param name="tempxmlfilepathtype">类型</param>
 87        public myxml( string tempxmlfilepath , enumxmlpathtype tempxmlfilepathtype )
 88        {
 89            //
 90            // todo: 在此处添加构造函数逻辑
 91            //
 92            this.xmlfilepathtype = tempxmlfilepathtype ;
 93            this.xmlfilepath = tempxmlfilepath ;
 94            getxmldocument() ;
 95        }
 96        #endregion
 97
 98
 99        /**////<summary>
100        ///获取xmldocument实体类
101        ///</summary>   
102        /// <returns>指定的xml描述文件的一个xmldocument实例</returns>
103        private xmldocument getxmldocument()
104        {
105            xmldocument doc=null;
106
107            if( this.xmlfilepathtype == enumxmlpathtype.absolutepath )
108            {
109                doc = getxmldocumentfromfile( xmlfilepath ) ;
110            }
111            else if( this.xmlfilepathtype == enumxmlpathtype.virtualpath )
112            {
113                doc = getxmldocumentfromfile(httpcontext.current.server.mappath(xmlfilepath)) ;
114            }
115            return doc;
116        }
117
118        private xmldocument getxmldocumentfromfile(string tempxmlfilepath)
119        {
120            string xmlfilefullpath = tempxmlfilepath ;
121            xmldoc.load(xmlfilefullpath) ;
122            //定义事件处理
123            xmldoc.nodechanged += new xmlnodechangedeventhandler(this.nodeupdateevent);
124            xmldoc.nodeinserted += new xmlnodechangedeventhandler(this.nodeinsertevent);
125            xmldoc.noderemoved += new xmlnodechangedeventhandler(this.nodedeleteevent);
126            return xmldoc ;
127        }
128
129        读取指定节点的指定属性值#region 读取指定节点的指定属性值
130        /**//// <summary>
131        /// 功能:
132        /// 读取指定节点的指定属性值   
133        /// </summary>
134        /// <param name="strnode">节点名称</param>
135        /// <param name="strattribute">此节点的属性</param>
136        /// <returns></returns>
137        public string getxmlnodeattributevalue(string strnode,string strattribute)
138        {
139            string strreturn = "";
140            try
141            {
142                //根据指定路径获取节点
143                xmlnode xmlnode = xmldoc.selectsinglenode(strnode) ;
144                if (!(xmlnode==null))
145                {//获取节点的属性,并循环取出需要的属性值
146                    xmlattributecollection xmlattr = xmlnode.attributes ;
147
148                    for(int i=0 ;i<xmlattr.count; i++)
149                    {
150                        if (xmlattr.item(i).name == strattribute)
151                        {
152                            strreturn = xmlattr.item(i).value ;
153                            break;
154                        }
155                    }
156                }
157            }
158            catch(xmlexception xmle)
159            {
160                throw xmle ;
161            }
162            return strreturn ;
163        }
164        #endregion
165
166
167        读取指定节点的值#region 读取指定节点的值
168        /**//// <summary>
169        /// 功能:
170        /// 读取指定节点的值   
171        /// </summary>
172        /// <param name="strnode">节点名称</param>
173        /// <returns></returns>
174        public string getxmlnodevalue(string strnode)
175        {
176            string strreturn = string.empty ;
177
178            try
179            {
180                //根据路径获取节点
181                xmlnode xmlnode = xmldoc.selectsinglenode(strnode) ;
182                if (!(xmlnode==null))
183                    strreturn = xmlnode.innertext ;
184            }
185            catch(xmlexception xmle)
186            {
187                throw xmle ;
188            }
189            return strreturn ;
190        }
191        #endregion
192
193        设置节点值#region 设置节点值
194        /**//// <summary>
195        /// 功能:
196        /// 设置节点值       
197        /// </summary>
198        /// <param name="strnode">节点的名称</param>
199        /// <param name="newvalue">节点值</param>
200        public void setxmlnodevalue(string xmlnodepath,string xmlnodevalue)
201        {
202            try
203            {
204                //可以批量为符合条件的节点进行付值
205                xmlnodelist xmlnode=this.xmldoc.selectnodes(xmlnodepath);
206                if (!(xmlnode==null))
207                {
208                    foreach(xmlnode xn in xmlnode)
209                    {
210                        xn.innertext = xmlnodevalue ;   
211                    }
212                }
213                /**//*
214                 * 根据指定路径获取节点
215                xmlnode xmlnode = xmldoc.selectsinglenode(xmlnodepath) ;           
216                //设置节点值
217                if (!(xmlnode==null))
218                    xmlnode.innertext = xmlnodevalue ;*/               
219            }
220            catch(xmlexception xmle)
221            {
222                throw xmle ;
223            }
224        }
225        #endregion
226
227        设置节点的属性值#region 设置节点的属性值
228        /**//// <summary>
229        /// 功能:
230        /// 设置节点的属性值   
231        /// </summary>
232        /// <param name="xmlnodepath">节点名称</param>
233        /// <param name="xmlnodeattribute">属性名称</param>
234        /// <param name="xmlnodeattributevalue">属性值</param>
235        public void setxmlnodeattributevalue(string xmlnodepath,string xmlnodeattribute,string xmlnodeattributevalue)
236        {
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                }
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                           
325            }
326            catch(xmlexception xmle)
327            {
328                throw xmle;
329            }
330        }
331       
332        /**//// <summary>
333        /// 向一个节点添加属性
334        /// </summary>
335        /// <param name="nodepath">节点路径</param>
336        /// <param name="nodeattribute">属性名</param>
337        public void addattribute( string nodepath , string nodeattribute)
338        {
339            privateaddattribute(nodepath,nodeattribute,"");
340        }
341        /**//// <summary>
342        ///
343        /// </summary>
344        /// <param name="nodepath"></param>
345        /// <param name="nodeattribute"></param>
346        /// <param name="nodeattributevalue"></param>
347        private void privateaddattribute( string nodepath , string nodeattribute,string nodeattributevalue)
348        {
349            try
350            {
351                xmlnode nodepath = xmldoc.selectsinglenode( nodepath ) ;
352                if (!(nodepath==null))
353                {   
354                    xmlattribute nodeattribute = this.xmldoc.createattribute(nodeattribute);
355                    nodeattribute.value=nodeattributevalue;
356                    nodepath.attributes.append(nodeattribute) ;                       
357                }
358            }
359            catch(xmlexception xmle)
360            {
361                throw xmle;
362            }
363        }
364        /**//// <summary>
365        ///  向一个节点添加属性,并付值
366        /// </summary>
367        /// <param name="nodepath">节点</param>
368        /// <param name="nodeattribute">属性名</param>
369        /// <param name="nodeattributevalue">属性值</param>
370        public void addattribute( string nodepath , string nodeattribute,string nodeattributevalue)
371        {
372            privateaddattribute(nodepath,nodeattribute,nodeattributevalue);
373        }
374        #endregion
375
376        删除#region 删除
377        /**//// <summary>
378        /// 删除节点的一个属性
379        /// </summary>
380        /// <param name="nodepath">节点所在的xpath表达式</param>
381        /// <param name="nodeattribute">属性名</param>
382        public void deleteattribute( string nodepath , string nodeattribute)
383        {           
384            xmlnodelist nodepath =this.xmldoc.selectnodes(nodepath);           
385            if (!(nodepath==null))
386            {
387                foreach (xmlnode tempxn in nodepath)
388                {
389                    xmlattributecollection xmlattr = tempxn.attributes ;
390                    for(int i=0 ; i<xmlattr.count ; i++)
391                    {
392                        if ( xmlattr.item(i).name == nodeattribute)
393                        {
394                            tempxn.attributes.removeat(i);
395                            break ;
396                        }
397                    }
398                }
399            }
400        }
401       
402        /**//// <summary>
403        /// 删除节点,当其属性值等于给定的值时
404        /// </summary>
405        /// <param name="nodepath">节点所在的xpath表达式</param>
406        /// <param name="nodeattribute">属性</param>
407        /// <param name="nodeattributevalue">值</param>
408        public void deleteattribute( string nodepath , string nodeattribute , string nodeattributevalue)
409        {
410            xmlnodelist nodepath =this.xmldoc.selectnodes(nodepath);           
411            if (!(nodepath==null))
412            {
413                foreach (xmlnode tempxn in nodepath)
414                {
415                    xmlattributecollection xmlattr = tempxn.attributes ;
416                    for(int i=0 ; i<xmlattr.count ; i++)
417                    {
418                        if ( xmlattr.item(i).name == nodeattribute && xmlattr.item(i).value==nodeattributevalue)
419                        {
420                            tempxn.attributes.removeat(i);
421                            break ;
422                        }
423                    }
424                }
425            }
426        }
427        /**//// <summary>
428        /// 删除节点
429        /// </summary>
430        /// <param name="tempxmlnode"></param>
431        /// <remarks></remarks>
432        public void deletexmlnode(string tempxmlnode){   
433            xmlnodelist nodepath =this.xmldoc.selectnodes(tempxmlnode);
434            if (!(nodepath==null))
435            {
436                foreach(xmlnode xn in nodepath)
437                {
438                    xn.parentnode.removechild(xn);       
439                }
440            }
441        }
442
443        #endregion
444
445        xml文档事件#region xml文档事件
446        /**//// <summary>
447        ///
448        /// </summary>
449        /// <param name="src"></param>
450        /// <param name="args"></param>
451        private  void nodeinsertevent(object src, xmlnodechangedeventargs args)
452        {
453            //保存设置
454            savexmldocument();
455        }
456        /**//// <summary>
457        ///
458        /// </summary>
459        /// <param name="src"></param>
460        /// <param name="args"></param>
461        private  void nodedeleteevent(object src, xmlnodechangedeventargs args)
462        {
463            //保存设置
464            savexmldocument();
465        }
466        /**//// <summary>
467        ///
468        /// </summary>
469        /// <param name="src"></param>
470        /// <param name="args"></param>
471        private  void nodeupdateevent(object src, xmlnodechangedeventargs args)
472        {
473            //保存设置
474            savexmldocument();
475        }
476        #endregion
477
478        保存xml文件#region 保存xml文件
479        /**//// <summary>
480        /// 功能:
481        /// 保存xml文件
482        ///
483        /// </summary>
484        public void savexmldocument()
485        {
486            try
487            {
488                //保存设置的结果
489                if( this.xmlfilepathtype == enumxmlpathtype.absolutepath )
490                {
491                    savexml( xmlfilepath ) ;
492                }
493                else if( this.xmlfilepathtype == enumxmlpathtype.virtualpath )
494                {
495                    savexml(httpcontext.current.server.mappath(xmlfilepath)) ;
496                }
497            }
498            catch(xmlexception xmle)
499            {
500                throw xmle;
501            }
502        }
503   
504        /**//// <summary>
505        /// 功能:
506        /// 保存xml文件   
507        /// </summary>
508        public void savexmldocument(string tempxmlfilepath)
509        {
510            try
511            {
512                //保存设置的结果
513                savexml(tempxmlfilepath);
514            }
515            catch(xmlexception xmle)
516            {
517                throw xmle;
518            }
519        }
520        /**//// <summary>
521        ///
522        /// </summary>
523        /// <param name="filepath"></param>
524        private void savexml(string filepath)
525        {
526            xmldoc.save(filepath);
527        }
528
529        #endregion
530
531    }
532
533}
534
535


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·基于cpld的数字触发电路的设计
·消息队列(Message Queue)简介及其使用-.NET教程,评论及其它
·session_register()出错的解决办法-PHP教程,其它文章
·卫星通信中tcp协议分析改进方法研究
·漫谈Java语言的接口与类型安全-JSP教程,Java技巧及代码
·SQL SERVER2005連接字串中的@3/29-.NET教程,数据库应用
·自创]JCreator安装学习使用方法-数据库专栏,SQL Server
·ASP.NET 数据绑定常用代码-.NET教程,Asp.Net开发
·如何在J2ME的低级界面中轻松实现各种文字的自然分行显示-JSP教程,J2ME开发
·通信设备pac模块式开关电源的原理与维修
最新文章
·photoshop将肖像照片处理为铅笔素描_photoshop教程
·个人网站做联盟广告的几点经验_网赚技巧
·适合与不适合做google adsense的站_网赚技巧
·gg网赚之:怎么样利用e文站轻松月入100刀_网赚技巧
·黄明明归国创业寻觅伙伴:人品好是必要条件_站长访谈
·最普通的7种软文类型_站长访谈
·第九城市ceo朱骏 网海中闯出一片天_站长访谈
·反波访谈:听keso乱弹琴_站长访谈
·人性和互联网_站长心得
·大型网站常用的五种推广方法_站长心得
相关主题
  • .net操纵xml文件类(c#)_asp.net技巧
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭