zoukankan      html  css  js  c++  java
  • 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         }
  • 相关阅读:
    POJ 1469 COURSES 二分图最大匹配
    POJ 1325 Machine Schedule 二分图最大匹配
    USACO Humble Numbers DP?
    SGU 194 Reactor Cooling 带容量上下限制的网络流
    POJ 3084 Panic Room 求最小割
    ZOJ 2587 Unique Attack 判断最小割是否唯一
    Poj 1815 Friendship 枚举+求最小割
    POJ 3308 Paratroopers 最小点权覆盖 求最小割
    1227. Rally Championship
    Etaoin Shrdlu
  • 原文地址:https://www.cnblogs.com/dabiaoge/p/4202218.html
Copyright © 2011-2022 走看看