C# XmlReader

一个非常全面的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         }
时间: 2024-10-10 13:49:41

C# XmlReader的相关文章

使用XmlReader读取xml文件之二

在.net开发中经常需要读写xml形式的文件(app.config和web.config分别是WinForm和WebForm中使用到的 xml文件的一个特列,并且微软提供了通用的方法,在此就不赘述了), .net类库提供了多种读写xml文件的方式,每一种方式都有其优点和 缺点,因而有其实用性. 下面列出微软.net类库提供的读写xml文件个类及其特点: 类名称 优点 缺点 XmlReader 快速.高效.可扩展 只读,只向前,需要人工验证 XmlDocument 可往返.可读写.支持XPath筛

php xml 文件读取 XMLReader

php xml 文件读取 <?php /** $xmlString = '<xml> <persons count="10"> <person username="username1" age="20">this is username1 description</person> <person username="username2" age="20&qu

深入认识XmlReader

深入认识XmlReader 摘要 XmlReader类是组成.NET的关键技术之一,极大地方便了开发人员对Xml的操作.通过本文您将对XmlReader有一个很好的认识,并将其应用到实际开发中. 目录 概要 创建Xml读取器 访问外部资源 读取数据 一个简单的实例 1.概要 XmlReader 类是一个提供对 XML 数据的非缓存.只进只读访问的抽象基类.该类符合 W3C 可扩展标记语言 (XML) 1.0 和 XML 中的命名空间的建议. XmlReader 类支持从流或文件读取 XML 数据

c# 操作xml之xmlReader

xmlReader的名称空间using System.Xml; xmlReader是通过流的方式来读取xml文件的内容 <?xml version="1.0" encoding="utf-8" ?><!--<!-–This file represents a fragment of a book store inventory database-–>--><bookstore>  <book genre=&quo

XmlReader在未知元素的名称和属性的名称的情况下读取属性

经过昨天到今天的努力以及博问上好心人的帮助,终于解决了XmlReader在未知元素的名称和属性的名称的情况下读取属性的方法. 在没有解决前,我的代码如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml; 6 7 namespace ReadAttribute 8 { 9 class Program 10 { 11 st

C#操作Xml:使用XmlReader读Xml

XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和IO的开销,这时候就该XmlReader和XmlWriter出场了. XmlReader读取Xml需要通过Read()实例方法,不断读取Xml文档中的声明,节点开始,节点内容,节点结束,以及空白等等,直到文档结束,Read()方法返回false. 如下读取Xml内容实例代码和注释说明 ? //玉开技术

XmlReader在序列化中的使用

和XmlDocument最大的不同——XmlReader逐行读取.单独很少使用,一般配合序列化(反序列化)使用,以下给出具体例子: namespace ConsoleApplication1 { public class CData : IXmlSerializable { private string cDataValue = null; public string CDataValue { get { return cDataValue; } private set { cDataValue

C# XmlReader/XmlWriter 类

XmlReader用于读取Xml文件,XmlWriter用于将数据写到Xml文件.其实,在印象当中,XML很多的操作类都支持直接Save.Read也支持接受XmlReader与XmlWriter类的示例作为参数,但是为什么还要有这个两类来专门用于读写XML文件呢?因为它们有强大的自定义格式功能: 一.XmlReader的使用 XmlReader类专门用于读取Xml文件,最大的特点在于支持Settings. 属性 说明 AttributeCount 当在派生类中被重写时,获取当前节点上的属性数 B

使用XMLReader接口解析XML-个人记录

如何获取XMLReader接口 从JDK文档来看,有两种方法: 使用SAXParserFactory+SAXParser[以下简称a方法] XMLReader aaa=SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 使用XMLReaderFactory[以下简称b方法] XMLReader bbb=XMLReaderFactory.createXMLReader(); 两种方法不同之处 在JDK文档a方法中SAXPars