一个非常全面的XML解析类

  1 using System;
  2 using UnityEngine;
  3 using System.Xml;
  4 using System.Collections;
  5
  6 using UnityObject = UnityEngine.Object;
  7 using SystemObject = System.Object;
  8
  9 using Fcm;
 10
 11 using LoadedTexts =
 12     System.Collections.Generic.Dictionary<
 13         System.String,
 14         System.String
 15     >;
 16
 17 public sealed class XmlReader
 18 {
 19     public Boolean Open(string fileName)
 20     {
 21         Close();
 22
 23         try
 24         {
 25             _Document = new XmlDocument();
 26             String xml_content = XmlContent(fileName);
 27             _Document.LoadXml(xml_content);
 28             XmlNodeList list = _Document.GetElementsByTagName("fcm");
 29             if (0 >= list.Count)
 30                 return (false);
 31             _Root = list[0] as XmlElement;
 32         }
 33         catch (Exception)
 34         {
 35             return (false);
 36         }
 37
 38         return (true);
 39     }
 40
 41     public Int32 ConfigCount
 42     {
 43         get
 44         {
 45             try
 46             {
 47                 if (null == _Root)
 48                     return (0);
 49                 return (Int32.Parse(_Root.Attributes["count"].Value));
 50             }
 51             catch (Exception)
 52             {
 53                 return (0);
 54             }
 55         }
 56     }
 57
 58     public Boolean SeekConfig()
 59     {
 60         return (SeekConfig(0));
 61     }
 62
 63     public Boolean SeekConfig(Int32 config_index)
 64     {
 65         if (null == _Document || null == _Root)
 66             return (false);
 67
 68         String config_name = String.Format("CFG{0}", config_index);
 69         try
 70         {
 71             _CurrentConfig = _Root[config_name];
 72         }
 73         catch (Exception)
 74         {
 75             return (false);
 76         }
 77
 78         return (true);
 79     }
 80
 81     public Int32 RecordCount
 82     {
 83         get
 84         {
 85             try
 86             {
 87                 if (null == _CurrentConfig)
 88                     return (0);
 89                 return (Int32.Parse(_CurrentConfig.Attributes["count"].Value));
 90             }
 91             catch (Exception)
 92             {
 93                 return (0);
 94             }
 95         }
 96     }
 97
 98     public Boolean SeekRecord(Int32 record_index)
 99     {
100         if (null == _Document || null == _Root || null == _CurrentConfig)
101             return (false);
102
103         String record_name = String.Format("RECORD{0}", record_index);
104         try
105         {
106             _CurrentRecord = _CurrentConfig[record_name];
107         }
108         catch (Exception)
109         {
110             return (false);
111         }
112
113         return (true);
114     }
115
116     public Boolean SeekNextRecord()
117     {
118         if (null == _Document || null == _Root || null == _CurrentConfig)
119             return (false);
120
121         try
122         {
123             if (null == _CurrentRecords)
124             {
125                 XmlNodeList nl = _CurrentConfig.ChildNodes;
126                 _CurrentRecords = nl.GetEnumerator();
127             }
128             if (!_CurrentRecords.MoveNext())
129                 return (false);
130             _CurrentRecord = (XmlElement)_CurrentRecords.Current;
131         }
132         catch (Exception)
133         {
134             return (false);
135         }
136
137         return (true);
138     }
139
140     public String RecordString(String field_name)
141     {
142         return (RecordString(field_name, ""));
143     }
144
145     public String RecordString(String field_name, String def)
146     {
147         if (null == _CurrentRecord)
148             return (def);
149
150         try
151         {
152
153             XmlElement e = _CurrentRecord[field_name];
154             if (null == e)
155                 return (def);
156
157             return (e.InnerText);
158
159         }
160         catch (Exception)
161         {
162
163             return (def);
164         }
165     }
166
167     public Int16 RecordInt16(String field_name)
168     {
169         return (RecordInt16(field_name, 0));
170     }
171
172     public Int16 RecordInt16(String field_name, Int16 def)
173     {
174         if (null == _CurrentRecord)
175             return (0);
176
177         String str = RecordString(field_name);
178         try
179         {
180             Int16 v = Int16.Parse(str);
181             return (v);
182         }
183         catch (Exception)
184         {
185             return (def);
186         }
187     }
188
189     public Int32 RecordInt(String field_name)
190     {
191         return (RecordInt(field_name, 0));
192     }
193
194     public Int32 RecordInt(String field_name, Int32 def)
195     {
196         if (null == _CurrentRecord)
197             return (0);
198
199         String str = RecordString(field_name);
200         try
201         {
202             Int32 v = Int32.Parse(str);
203             return (v);
204         }
205         catch (Exception)
206         {
207             return (def);
208         }
209     }
210
211     public Int64 RecordInt64(String field_name)
212     {
213         return (RecordInt64(field_name, 0));
214     }
215
216     public Int64 RecordInt64(String field_name, Int64 def)
217     {
218         if (null == _CurrentRecord)
219             return (0);
220
221         String str = RecordString(field_name);
222         try
223         {
224             Int64 v = Int64.Parse(str);
225             return (v);
226         }
227         catch (Exception)
228         {
229             return (def);
230         }
231     }
232
233     public String[] RecordStringArray(String field_name)
234     {
235         return (RecordStringArray(field_name, ",", 0));
236     }
237
238     public String[] RecordStringArray(String field_name, Int32 match_count)
239     {
240         return (RecordStringArray(field_name, ",", match_count));
241     }
242
243     public String[] RecordStringArray(String field_name, String split)
244     {
245         return (RecordStringArray(field_name, split, 0));
246     }
247
248     public String[] RecordStringArray(String field_name, String split, Int32 match_count)
249     {
250         if (null == _CurrentRecord)
251             return (new String[0]);
252
253         String str = RecordString(field_name);
254
255         String[] splits = str.Split(split.ToCharArray());
256         Int32 split_count = splits.Length;
257
258         if (0 < match_count && match_count != split_count)
259             return (new String[0]);
260
261         if (1 == split_count && 0 >= splits[0].Length)
262             split_count = 0;
263
264         try
265         {
266             String[] ar = new String[split_count];
267             for (Int32 i = 0; i < split_count; i++)
268                 ar[i] = splits[i];
269             return (ar);
270         }
271         catch (Exception)
272         {
273             return (new String[0]);
274         }
275     }
276
277     public String[][] RecordStringArray2(String field_name, String split1, String split2)
278     {
279         if (null == _CurrentRecord)
280             return (new String[0][]);
281
282         String str = RecordString(field_name);
283
284         String[] splits1 = str.Split(split1.ToCharArray());
285         Int32 split_count1 = splits1.Length;
286
287         if (1 == split_count1 && 0 >= splits1[0].Length)
288             split_count1 = 0;
289
290         try
291         {
292             String[][] ar = new String[split_count1][];
293             for (Int32 i = 0; i < split_count1; i++)
294             {
295                 String s = splits1[i];
296                 String[] splits2 = s.Split(split2.ToCharArray());
297                 Int32 split_count2 = splits2.Length;
298                 if (1 == split_count2 && 0 >= splits2[0].Length)
299                     split_count2 = 0;
300                 ar[i] = new String[split_count2];
301                 for (Int32 j = 0; j < split_count2; j++)
302                 {
303                     ar[i][j] = splits2[j];
304                 }
305             }
306             return (ar);
307
308         }
309         catch (Exception)
310         {
311             return (new String[0][]);
312         }
313     }
314
315     public Int32[] RecordIntArray(String field_name)
316     {
317         return (RecordIntArray(field_name, ",", 0));
318     }
319
320     public Int32[] RecordIntArray(String field_name, Int32 match_count)
321     {
322         return (RecordIntArray(field_name, ",", match_count));
323     }
324
325     public Int32[] RecordIntArray(String field_name, String split)
326     {
327         return (RecordIntArray(field_name, split, 0));
328     }
329
330     public Int32[] RecordIntArray(String field_name, String split, Int32 match_count)
331     {
332         if (null == _CurrentRecord)
333             return (new Int32[0]);
334
335         String str = RecordString(field_name);
336
337         String[] splits = str.Split(split.ToCharArray());
338         Int32 split_count = splits.Length;
339
340         if (0 < match_count && match_count != split_count)
341             return (new Int32[0]);
342
343         if (1 == split_count && 0 >= splits[0].Length)
344             split_count = 0;
345
346         try
347         {
348             Int32[] ar = new Int32[split_count];
349             for (Int32 i = 0; i < split_count; i++)
350                 ar[i] = Int32.Parse(splits[i]);
351             return (ar);
352         }
353         catch (Exception)
354         {
355             return (new Int32[0]);
356         }
357     }
358
359     public Int32[][] RecordIntArray2(String field_name, String split1, String split2)
360     {
361         if (null == _CurrentRecord)
362             return (new Int32[0][]);
363
364         String str = RecordString(field_name);
365
366         String[] splits1 = str.Split(split1.ToCharArray());
367         Int32 split_count1 = splits1.Length;
368
369         if (1 == split_count1 && 0 >= splits1[0].Length)
370             split_count1 = 0;
371
372         try
373         {
374             Int32[][] ar = new Int32[split_count1][];
375             for (Int32 i = 0; i < split_count1; i++)
376             {
377                 String s = splits1[i];
378                 String[] splits2 = s.Split(split2.ToCharArray());
379                 Int32 split_count2 = splits2.Length;
380                 if (1 == split_count2 && 0 >= splits2[0].Length)
381                     split_count2 = 0;
382                 ar[i] = new Int32[split_count2];
383                 for (Int32 j = 0; j < split_count2; j++)
384                 {
385                     ar[i][j] = Int32.Parse(splits2[j]);
386                 }
387             }
388             return (ar);
389
390         }
391         catch (Exception)
392         {
393             return (new Int32[0][]);
394         }
395     }
396
397     public Int64[] RecordInt64Array(String field_name)
398     {
399         return (RecordInt64Array(field_name, ",", 0));
400     }
401
402     public Int64[] RecordInt64Array(String field_name, Int64 match_count)
403     {
404         return (RecordInt64Array(field_name, ",", match_count));
405     }
406
407     public Int64[] RecordInt64Array(String field_name, String split)
408     {
409         return (RecordInt64Array(field_name, split, 0));
410     }
411
412     public Int64[] RecordInt64Array(String field_name, String split, Int64 match_count)
413     {
414         if (null == _CurrentRecord)
415             return (new Int64[0]);
416
417         String str = RecordString(field_name);
418
419         String[] splits = str.Split(split.ToCharArray());
420         Int32 split_count = splits.Length;
421
422         if (0 < match_count && match_count != split_count)
423             return (new Int64[0]);
424
425         if (1 == split_count && 0 >= splits[0].Length)
426             split_count = 0;
427
428         try
429         {
430             Int64[] ar = new Int64[split_count];
431             for (Int32 i = 0; i < split_count; i++)
432                 ar[i] = Int64.Parse(splits[i]);
433             return (ar);
434         }
435         catch (Exception)
436         {
437             return (new Int64[0]);
438         }
439     }
440
441     public Int64[][] RecordInt64Array2(String field_name, String split1, String split2)
442     {
443         if (null == _CurrentRecord)
444             return (new Int64[0][]);
445
446         String str = RecordString(field_name);
447
448         String[] splits1 = str.Split(split1.ToCharArray());
449         Int32 split_count1 = splits1.Length;
450
451         if (1 == split_count1 && 0 >= splits1[0].Length)
452             split_count1 = 0;
453
454         try
455         {
456             Int64[][] ar = new Int64[split_count1][];
457             for (Int32 i = 0; i < split_count1; i++)
458             {
459                 String s = splits1[i];
460                 String[] splits2 = s.Split(split2.ToCharArray());
461                 Int32 split_count2 = splits2.Length;
462                 if (1 == split_count2 && 0 >= splits2[0].Length)
463                     split_count2 = 0;
464                 ar[i] = new Int64[split_count2];
465                 for (Int32 j = 0; j < split_count2; j++)
466                 {
467                     ar[i][j] = Int64.Parse(splits2[j]);
468                 }
469             }
470             return (ar);
471
472         }
473         catch (Exception)
474         {
475             return (new Int64[0][]);
476         }
477     }
478
479     public void Close()
480     {
481         _CurrentRecord = null;
482         _CurrentRecords = null;
483         _CurrentConfig = null;
484         _Root = null;
485         _Document = null;
486     }
487
488     public String XmlPath(String file_title)
489     {
490         return (String.Format("Xml/{0}", file_title));
491     }
492
493     public String XmlContent(String file_title)
494     {
495         return (LoadText(XmlPath(file_title)));
496     }
497
498     public String LoadText(String name)
499     {
500         String text;
501         lock (_LoadedTexts)
502         {
503             if (_LoadedTexts.TryGetValue(name, out text))
504                 return (text);
505         }
506
507         UnityObject obj = Resources.Load(name);
508         if (null == obj)
509             return ("");
510         if (!(obj is TextAsset))
511             return ("");
512         text = obj.ToString();
513
514         lock (_LoadedTexts)
515         {
516             _LoadedTexts.Add(name, text);
517         }
518
519         return (text);
520     }
521
522     private XmlDocument _Document;
523     private XmlElement _Root;
524     private XmlElement _CurrentConfig;
525     private IEnumerator _CurrentRecords = null;
526     private XmlElement _CurrentRecord;
527
528     private LoadedTexts _LoadedTexts = new LoadedTexts();
529 }

xml文件范例

 1 <?xml version="1.0"?>
 2 <fcm count="1">
 3     <CFG0 count="2">
 4     <RECORD0>
 5       <area_id>1</area_id>
 6       <area_name>1区 狮子座</area_name>
 7       <area_state>新区</area_state>
 8       <state_code>0</state_code>
 9     </RECORD0>
10     <RECORD1>
11       <area_id>2</area_id>
12       <area_name>2区 狮子座</area_name>
13       <area_state>新区</area_state>
14       <state_code>0</state_code>
15     </RECORD1>
16     </CFG0>
17 </fcm>

使用范例

 1         public override void OnInitialize()
 2         {
 3             XmlReader = new XmlReader ();
 4             if ( cfg.Open( "areas" ) )
 5             {
 6                 cfg.SeekConfig();
 7                 Int32 record_count = cfg.RecordCount;
 8                 for ( Int32 i = 0; i < record_count; i++ )
 9                 {
10                     if ( cfg.SeekNextRecord() )
11                     {
12                         Int32 area_id= cfg.RecordInt( "area_id" );
13                         String area_name= cfg.RecordString( "area_name" );
14                     }
15                 }
16             }
17             else
18             {
19                 String title = "No Exit Xml file : ";
20                 title += TableConfig.XmlTitle( TableConfigType.Resource );
21                 UnityEngine.Debug.LogWarning( "" + title );
22             }
23             cfg.Close();
24         }

转载于:https://www.cnblogs.com/dabiaoge/p/4202218.html

C# XmlReader相关推荐

  1. [导入]XmlReader 读取器读取内存流 MemoryStream 的注意事项

    MemoryStream对象提供了无需进行IO就可以创建Stream的方法,XmlTextWriter和XmlReader提供快速书写和读取XML内容的方法,结合MemoryStream,就可以直接在 ...

  2. ASP.NET 2.0 XML 系列(4):用XmlReader类介绍

    (本文摘自MSDN) XmlReader 类是一个提供对 XML 数据的非缓存.只进只读访问的抽象基类.该类符合 W3C 可扩展标记语言 (XML) 1.0 和 XML 中的命名空间的建议. XmlR ...

  3. 使用XmlReader读Xml

    XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...

  4. ASP.NET2.0 XML系列(6): 使用XmlReaderSettings配置XmlReader的输出

    XmlReaderSettings是用来验证XML数据的一个重要的类,后面我们将用这个类来验证XML文件,这里我们先演示使用XmlReaderSettings来配置XmlReader的输出 XmlRe ...

  5. php xmlreader 读xml,PHP中使用xmlreader读取xml数据示例

    这篇文章主要介绍了PHP中使用xmlreader读取xml数据示例,本文示例相对简单,只包含了一个读取功能,需要的朋友可以参考下 有一个XML文件,,内容如下: 复制代码 代码如下: Simpsons ...

  6. php xml getattribute,PHP XMLReader getAttribute()用法及代码示例

    XMLReader::getAttribute()函数是PHP中的一个内置函数,用于获取命名属性的值. 用法: string XMLReader::getAttribute( string $name ...

  7. xmlreader php 读取某节点,php – 使用XMLReader选择父节点

    我不得不重写程序的一部分,使用XMLReader选择XML文件的一部分进行处理. 以这个简化的XML为例: 这次调用xpath(): $bets = $xml->xpath( "//o ...

  8. XmlReader 使用

    <Root><Phone Name="Oppo手机"><BackGround Description="颜色">白色 黑色 ...

  9. XmlReader 内部使用NameTable

    有些类(如 XmlDocument 和 XmlReader)在内部使用 NameTable 类存储属性名和元素名.当 XML 文档中多次出现某个元素名或属性名时,该名称在 NameTable 中只存储 ...

  10. XMLReader—一个可以读取XML文件的java类

    在我的博文java ArrayList代替数组中有个XMLReader类,这是一个可以读取XML文件的java类,它的实现过程是怎样的呢?接下来让我们看看XMLReader的实现过程: 首先你要下载一 ...

最新文章

  1. JavaScript——this、constructor、prototype
  2. LDD3源码分析之阻塞型I/O
  3. 论文公式编号右对齐_word技能之添加样式以及公式自动编号
  4. Linux零基础初级教程
  5. java动态定义泛型_关于oop:具有动态性质且可完全自定义的Java泛型编程
  6. linux系统中如何查看日志 (常用命令2)
  7. mysql主键异常(冲突)
  8. html检查输入为空,html input输入验证不为空
  9. Java连载1-概述常用的dos命令
  10. @经典微小说 因为爱
  11. 关于COM的Reg-Free(免注册)技术简介及实例讲解。
  12. 抖音群口令emoji表情转换群ID
  13. python 对字符串逆序输出
  14. 色相环上面的颜色和相邻颜色的关系是什么
  15. 诡异事件之自定义UINavigationController
  16. iPhone手机热点连接不稳定
  17. 蓝桥杯嵌入式 - 第九届决赛试题(电子秤)
  18. APIcloud的基本使用
  19. 美云智数营销云荣获“2021年中国轻工业数字化杰出解决方案”
  20. 复杂图形求面积c语言,用CAD计算复杂图形面积的方法

热门文章

  1. VC++:如何将程序最小化到托盘 [转]
  2. Linux下更换jdk和配置环境变量
  3. 用极限可以求瞬时速度的道理
  4. centos禁ping
  5. 求问模式达人,静态类可否代替单件?
  6. PAT甲级1091 Acute Stroke【三维bfs】
  7. P1651 塔 (动态规划)
  8. Mongo_安装 centos
  9. 使用 python 管理 mysql 开发工具箱 - 2
  10. java操作无符号数