在此类中用到了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 }