征途模拟器公用类之:XSotConfile
友情提示:博客园首发,本人享有示例源码的所有权,请在本人许可的前提下进行相关使用,否则保留追究法律责任的权利。另外,示例中所有素材均来源于网络,切勿用作商业用途,否则后果自负。如需转载,请注明出处。
XSotConfile,此类必须继承使用。本类实现了全局参数的解析标记为<global></global>,并把解析的参数保存在一个全局的参数容器global中如果用户有自己的配置,用户应该实现自己的参数解析。
从图中可以看出,SuperServer中的SuperConfile继承了XSotConfile并实现了ParseYour()。下面是XSotConfile代码:
View Code 1 using System.IO;
2 using System.Xml;
3
4 namespace XSot.Base
5 {
6 /// <summary>
7 /// 配置文件解析器
8 /// <para>此类必须继承使用。本类实现了全局参数的解析标记为 <global></global></para>
9 /// <para>如果用户有自己的配置,用户应该实现自己的参数解析</para>
10 /// </summary>
11 public abstract class XSotConfile
12 {
13 /// <summary>
14 /// xml文档
15 /// </summary>
16 protected XmlDocument m_xmlDocument;
17
18 /// <summary>
19 /// 配置文件名称
20 /// </summary>
21 protected string m_confile;
22
23 public XSotConfile()
24 : this("Config.xml")
25 {
26 }
27
28 public XSotConfile(string confile)
29 {
30 m_confile = confile;
31 m_xmlDocument = new XmlDocument();
32 }
33
34 /// <summary>
35 /// 全局解析函数
36 /// </summary>
37 /// <param name="nodeList">全局配置节点</param>
38 /// <returns>解析是否成功</returns>
39 protected bool GlobalParse(XmlNode nodeList)
40 {
41 foreach (XmlNode node in nodeList.ChildNodes)
42 {
43 if (node.Name == "SuperServer")
44 ParseSuperServer(node);
45 else
46 ParseNormal(node);
47 }
48 return true;
49 }
50
51 /// <summary>
52 /// 参数解析,会在global容器中放入两个参数
53 /// <para>Server SuperServer地址</para>
54 /// <para>Port SuperServer端口</para>
55 /// </summary>
56 /// <param name="node">SuperServer参数节点</param>
57 /// <returns>解析是否成功</returns>
58 protected bool ParseSuperServer(XmlNode node)
59 {
60 if (node.InnerText != "")
61 Zebra.Global["SuperServer"] = node.InnerText;
62 else
63 return false;
64 if (node.Attributes["Port"] != null)
65 Zebra.Global["Port"] = node.Attributes["Port"].Value;
66 else
67 Zebra.Global["Port"] = "10000";
68 return true;
69 }
70
71 /// <summary>
72 /// 普通参数解析,只是简单的把参数放入global容器中
73 /// </summary>
74 /// <param name="node">要解析的节点</param>
75 /// <returns>解析是否成功</returns>
76 protected bool ParseNormal(XmlNode node)
77 {
78 string result = node.InnerText;
79 if (node.Name == "mssql" || node.Name == "mysql")
80 {
81 if (node.Attributes["encode"] != null && node.Attributes["encode"].Value.ToUpper() == "YES")
82 {
83 result = Encrypt.zBase64.Base64_Decode(node.InnerText);
84 }
85 result = Data.SqlHelper.ParamUrl(result);
86 }
87 Zebra.Global[node.Name] = result;
88 return true;
89 }
90
91 /// <summary>
92 /// 开始解析配置文件
93 /// </summary>
94 /// <param name="name">使用者自己参数的定义节点名字</param>
95 /// <returns>解析是否成功</returns>
96 public bool Parse(string name)
97 {
98 if (!File.Exists(m_confile))
99 return false;
100 m_xmlDocument.Load(m_confile);
101 XmlElement root = (XmlElement)m_xmlDocument.SelectSingleNode("XSot");
102 if (root.HasChildNodes)
103 {
104 XmlNode globalNode = root.SelectSingleNode("global");
105 if (globalNode.HasChildNodes)
106 {
107 if (!GlobalParse(globalNode))
108 return false;
109 }
110 else
111 {
112 Zebra.logger.Warn("无全局配置段落。");
113 }
114 globalNode = root.SelectSingleNode(name);
115 if (globalNode != null)
116 return ParseYour(globalNode);
117 else
118 Zebra.logger.WarnFormat("无 {0} 配置段落", name);
119 }
120 return false;
121 }
122
123 protected abstract bool ParseYour(XmlNode nodeList);
124 }
125 }
2 using System.Xml;
3
4 namespace XSot.Base
5 {
6 /// <summary>
7 /// 配置文件解析器
8 /// <para>此类必须继承使用。本类实现了全局参数的解析标记为 <global></global></para>
9 /// <para>如果用户有自己的配置,用户应该实现自己的参数解析</para>
10 /// </summary>
11 public abstract class XSotConfile
12 {
13 /// <summary>
14 /// xml文档
15 /// </summary>
16 protected XmlDocument m_xmlDocument;
17
18 /// <summary>
19 /// 配置文件名称
20 /// </summary>
21 protected string m_confile;
22
23 public XSotConfile()
24 : this("Config.xml")
25 {
26 }
27
28 public XSotConfile(string confile)
29 {
30 m_confile = confile;
31 m_xmlDocument = new XmlDocument();
32 }
33
34 /// <summary>
35 /// 全局解析函数
36 /// </summary>
37 /// <param name="nodeList">全局配置节点</param>
38 /// <returns>解析是否成功</returns>
39 protected bool GlobalParse(XmlNode nodeList)
40 {
41 foreach (XmlNode node in nodeList.ChildNodes)
42 {
43 if (node.Name == "SuperServer")
44 ParseSuperServer(node);
45 else
46 ParseNormal(node);
47 }
48 return true;
49 }
50
51 /// <summary>
52 /// 参数解析,会在global容器中放入两个参数
53 /// <para>Server SuperServer地址</para>
54 /// <para>Port SuperServer端口</para>
55 /// </summary>
56 /// <param name="node">SuperServer参数节点</param>
57 /// <returns>解析是否成功</returns>
58 protected bool ParseSuperServer(XmlNode node)
59 {
60 if (node.InnerText != "")
61 Zebra.Global["SuperServer"] = node.InnerText;
62 else
63 return false;
64 if (node.Attributes["Port"] != null)
65 Zebra.Global["Port"] = node.Attributes["Port"].Value;
66 else
67 Zebra.Global["Port"] = "10000";
68 return true;
69 }
70
71 /// <summary>
72 /// 普通参数解析,只是简单的把参数放入global容器中
73 /// </summary>
74 /// <param name="node">要解析的节点</param>
75 /// <returns>解析是否成功</returns>
76 protected bool ParseNormal(XmlNode node)
77 {
78 string result = node.InnerText;
79 if (node.Name == "mssql" || node.Name == "mysql")
80 {
81 if (node.Attributes["encode"] != null && node.Attributes["encode"].Value.ToUpper() == "YES")
82 {
83 result = Encrypt.zBase64.Base64_Decode(node.InnerText);
84 }
85 result = Data.SqlHelper.ParamUrl(result);
86 }
87 Zebra.Global[node.Name] = result;
88 return true;
89 }
90
91 /// <summary>
92 /// 开始解析配置文件
93 /// </summary>
94 /// <param name="name">使用者自己参数的定义节点名字</param>
95 /// <returns>解析是否成功</returns>
96 public bool Parse(string name)
97 {
98 if (!File.Exists(m_confile))
99 return false;
100 m_xmlDocument.Load(m_confile);
101 XmlElement root = (XmlElement)m_xmlDocument.SelectSingleNode("XSot");
102 if (root.HasChildNodes)
103 {
104 XmlNode globalNode = root.SelectSingleNode("global");
105 if (globalNode.HasChildNodes)
106 {
107 if (!GlobalParse(globalNode))
108 return false;
109 }
110 else
111 {
112 Zebra.logger.Warn("无全局配置段落。");
113 }
114 globalNode = root.SelectSingleNode(name);
115 if (globalNode != null)
116 return ParseYour(globalNode);
117 else
118 Zebra.logger.WarnFormat("无 {0} 配置段落", name);
119 }
120 return false;
121 }
122
123 protected abstract bool ParseYour(XmlNode nodeList);
124 }
125 }
1 using System.Xml;
2 using XSot.Base;
3
4 namespace SuperServer
5 {
6 public class SuperConfile : XSotConfile
7 {
8 /// <summary>
9 /// 处理自己的字段
10 /// </summary>
11 /// <param name="nodeList">字段</param>
12 /// <returns></returns>
13 protected override bool ParseYour(XmlNode nodeList)
14 {
15 if (nodeList != null)
16 {
17 foreach (XmlNode node in nodeList)
18 ParseNormal(node);
19 return true;
20 }
21 return false;
22 }
23 }
24 }
2 using XSot.Base;
3
4 namespace SuperServer
5 {
6 public class SuperConfile : XSotConfile
7 {
8 /// <summary>
9 /// 处理自己的字段
10 /// </summary>
11 /// <param name="nodeList">字段</param>
12 /// <returns></returns>
13 protected override bool ParseYour(XmlNode nodeList)
14 {
15 if (nodeList != null)
16 {
17 foreach (XmlNode node in nodeList)
18 ParseNormal(node);
19 return true;
20 }
21 return false;
22 }
23 }
24 }
在MainForm中只需要
SuperConfile sc = new SuperConfile();
sc.Parse("SuperServer");
就可以读取Config.xml文件里<global></global>节点与<SuperServer></SuperServer>节点里的内容了。并把所有元素值都保存在Global的集合里,这个集合是唯一的。供全局调用。另外,SuperServer管着FLServer、InfoServer、RoleRegServer,同时影响其它程序。所以在<global></global>就是全局的。每个程序都需要调取他。

浙公网安备 33010602011771号