C# XmlReader

时间:2023-03-09 00:56:35
C# XmlReader
一个非常全面的XML解析类
 using System;
using UnityEngine;
using System.Xml;
using System.Collections; using UnityObject = UnityEngine.Object;
using SystemObject = System.Object; using Fcm; using LoadedTexts =
System.Collections.Generic.Dictionary<
System.String,
System.String
>; public sealed class XmlReader
{
public Boolean Open(string fileName)
{
Close(); try
{
_Document = new XmlDocument();
String xml_content = XmlContent(fileName);
_Document.LoadXml(xml_content);
XmlNodeList list = _Document.GetElementsByTagName("fcm");
if ( >= list.Count)
return (false);
_Root = list[] as XmlElement;
}
catch (Exception)
{
return (false);
} return (true);
} public Int32 ConfigCount
{
get
{
try
{
if (null == _Root)
return ();
return (Int32.Parse(_Root.Attributes["count"].Value));
}
catch (Exception)
{
return ();
}
}
} public Boolean SeekConfig()
{
return (SeekConfig());
} public Boolean SeekConfig(Int32 config_index)
{
if (null == _Document || null == _Root)
return (false); String config_name = String.Format("CFG{0}", config_index);
try
{
_CurrentConfig = _Root[config_name];
}
catch (Exception)
{
return (false);
} return (true);
} public Int32 RecordCount
{
get
{
try
{
if (null == _CurrentConfig)
return ();
return (Int32.Parse(_CurrentConfig.Attributes["count"].Value));
}
catch (Exception)
{
return ();
}
}
} public Boolean SeekRecord(Int32 record_index)
{
if (null == _Document || null == _Root || null == _CurrentConfig)
return (false); String record_name = String.Format("RECORD{0}", record_index);
try
{
_CurrentRecord = _CurrentConfig[record_name];
}
catch (Exception)
{
return (false);
} return (true);
} public Boolean SeekNextRecord()
{
if (null == _Document || null == _Root || null == _CurrentConfig)
return (false); try
{
if (null == _CurrentRecords)
{
XmlNodeList nl = _CurrentConfig.ChildNodes;
_CurrentRecords = nl.GetEnumerator();
}
if (!_CurrentRecords.MoveNext())
return (false);
_CurrentRecord = (XmlElement)_CurrentRecords.Current;
}
catch (Exception)
{
return (false);
} return (true);
} public String RecordString(String field_name)
{
return (RecordString(field_name, ""));
} public String RecordString(String field_name, String def)
{
if (null == _CurrentRecord)
return (def); try
{ XmlElement e = _CurrentRecord[field_name];
if (null == e)
return (def); return (e.InnerText); }
catch (Exception)
{ return (def);
}
} public Int16 RecordInt16(String field_name)
{
return (RecordInt16(field_name, ));
} public Int16 RecordInt16(String field_name, Int16 def)
{
if (null == _CurrentRecord)
return (); String str = RecordString(field_name);
try
{
Int16 v = Int16.Parse(str);
return (v);
}
catch (Exception)
{
return (def);
}
} public Int32 RecordInt(String field_name)
{
return (RecordInt(field_name, ));
} public Int32 RecordInt(String field_name, Int32 def)
{
if (null == _CurrentRecord)
return (); String str = RecordString(field_name);
try
{
Int32 v = Int32.Parse(str);
return (v);
}
catch (Exception)
{
return (def);
}
} public Int64 RecordInt64(String field_name)
{
return (RecordInt64(field_name, ));
} public Int64 RecordInt64(String field_name, Int64 def)
{
if (null == _CurrentRecord)
return (); String str = RecordString(field_name);
try
{
Int64 v = Int64.Parse(str);
return (v);
}
catch (Exception)
{
return (def);
}
} public String[] RecordStringArray(String field_name)
{
return (RecordStringArray(field_name, ",", ));
} public String[] RecordStringArray(String field_name, Int32 match_count)
{
return (RecordStringArray(field_name, ",", match_count));
} public String[] RecordStringArray(String field_name, String split)
{
return (RecordStringArray(field_name, split, ));
} public String[] RecordStringArray(String field_name, String split, Int32 match_count)
{
if (null == _CurrentRecord)
return (new String[]); String str = RecordString(field_name); String[] splits = str.Split(split.ToCharArray());
Int32 split_count = splits.Length; if ( < match_count && match_count != split_count)
return (new String[]); if ( == split_count && >= splits[].Length)
split_count = ; try
{
String[] ar = new String[split_count];
for (Int32 i = ; i < split_count; i++)
ar[i] = splits[i];
return (ar);
}
catch (Exception)
{
return (new String[]);
}
} public String[][] RecordStringArray2(String field_name, String split1, String split2)
{
if (null == _CurrentRecord)
return (new String[][]); String str = RecordString(field_name); String[] splits1 = str.Split(split1.ToCharArray());
Int32 split_count1 = splits1.Length; if ( == split_count1 && >= splits1[].Length)
split_count1 = ; try
{
String[][] ar = new String[split_count1][];
for (Int32 i = ; i < split_count1; i++)
{
String s = splits1[i];
String[] splits2 = s.Split(split2.ToCharArray());
Int32 split_count2 = splits2.Length;
if ( == split_count2 && >= splits2[].Length)
split_count2 = ;
ar[i] = new String[split_count2];
for (Int32 j = ; j < split_count2; j++)
{
ar[i][j] = splits2[j];
}
}
return (ar); }
catch (Exception)
{
return (new String[][]);
}
} public Int32[] RecordIntArray(String field_name)
{
return (RecordIntArray(field_name, ",", ));
} public Int32[] RecordIntArray(String field_name, Int32 match_count)
{
return (RecordIntArray(field_name, ",", match_count));
} public Int32[] RecordIntArray(String field_name, String split)
{
return (RecordIntArray(field_name, split, ));
} public Int32[] RecordIntArray(String field_name, String split, Int32 match_count)
{
if (null == _CurrentRecord)
return (new Int32[]); String str = RecordString(field_name); String[] splits = str.Split(split.ToCharArray());
Int32 split_count = splits.Length; if ( < match_count && match_count != split_count)
return (new Int32[]); if ( == split_count && >= splits[].Length)
split_count = ; try
{
Int32[] ar = new Int32[split_count];
for (Int32 i = ; i < split_count; i++)
ar[i] = Int32.Parse(splits[i]);
return (ar);
}
catch (Exception)
{
return (new Int32[]);
}
} public Int32[][] RecordIntArray2(String field_name, String split1, String split2)
{
if (null == _CurrentRecord)
return (new Int32[][]); String str = RecordString(field_name); String[] splits1 = str.Split(split1.ToCharArray());
Int32 split_count1 = splits1.Length; if ( == split_count1 && >= splits1[].Length)
split_count1 = ; try
{
Int32[][] ar = new Int32[split_count1][];
for (Int32 i = ; i < split_count1; i++)
{
String s = splits1[i];
String[] splits2 = s.Split(split2.ToCharArray());
Int32 split_count2 = splits2.Length;
if ( == split_count2 && >= splits2[].Length)
split_count2 = ;
ar[i] = new Int32[split_count2];
for (Int32 j = ; j < split_count2; j++)
{
ar[i][j] = Int32.Parse(splits2[j]);
}
}
return (ar); }
catch (Exception)
{
return (new Int32[][]);
}
} public Int64[] RecordInt64Array(String field_name)
{
return (RecordInt64Array(field_name, ",", ));
} public Int64[] RecordInt64Array(String field_name, Int64 match_count)
{
return (RecordInt64Array(field_name, ",", match_count));
} public Int64[] RecordInt64Array(String field_name, String split)
{
return (RecordInt64Array(field_name, split, ));
} public Int64[] RecordInt64Array(String field_name, String split, Int64 match_count)
{
if (null == _CurrentRecord)
return (new Int64[]); String str = RecordString(field_name); String[] splits = str.Split(split.ToCharArray());
Int32 split_count = splits.Length; if ( < match_count && match_count != split_count)
return (new Int64[]); if ( == split_count && >= splits[].Length)
split_count = ; try
{
Int64[] ar = new Int64[split_count];
for (Int32 i = ; i < split_count; i++)
ar[i] = Int64.Parse(splits[i]);
return (ar);
}
catch (Exception)
{
return (new Int64[]);
}
} public Int64[][] RecordInt64Array2(String field_name, String split1, String split2)
{
if (null == _CurrentRecord)
return (new Int64[][]); String str = RecordString(field_name); String[] splits1 = str.Split(split1.ToCharArray());
Int32 split_count1 = splits1.Length; if ( == split_count1 && >= splits1[].Length)
split_count1 = ; try
{
Int64[][] ar = new Int64[split_count1][];
for (Int32 i = ; i < split_count1; i++)
{
String s = splits1[i];
String[] splits2 = s.Split(split2.ToCharArray());
Int32 split_count2 = splits2.Length;
if ( == split_count2 && >= splits2[].Length)
split_count2 = ;
ar[i] = new Int64[split_count2];
for (Int32 j = ; j < split_count2; j++)
{
ar[i][j] = Int64.Parse(splits2[j]);
}
}
return (ar); }
catch (Exception)
{
return (new Int64[][]);
}
} public void Close()
{
_CurrentRecord = null;
_CurrentRecords = null;
_CurrentConfig = null;
_Root = null;
_Document = null;
} public String XmlPath(String file_title)
{
return (String.Format("Xml/{0}", file_title));
} public String XmlContent(String file_title)
{
return (LoadText(XmlPath(file_title)));
} public String LoadText(String name)
{
String text;
lock (_LoadedTexts)
{
if (_LoadedTexts.TryGetValue(name, out text))
return (text);
} UnityObject obj = Resources.Load(name);
if (null == obj)
return ("");
if (!(obj is TextAsset))
return ("");
text = obj.ToString(); lock (_LoadedTexts)
{
_LoadedTexts.Add(name, text);
} return (text);
} private XmlDocument _Document;
private XmlElement _Root;
private XmlElement _CurrentConfig;
private IEnumerator _CurrentRecords = null;
private XmlElement _CurrentRecord; private LoadedTexts _LoadedTexts = new LoadedTexts();
}

xml文件范例

 <?xml version="1.0"?>
<fcm count="1">
<CFG0 count="2">
<RECORD0>
<area_id>1</area_id>
<area_name>1区 狮子座</area_name>
<area_state>新区</area_state>
<state_code>0</state_code>
</RECORD0>
<RECORD1>
<area_id>2</area_id>
<area_name>2区 狮子座</area_name>
<area_state>新区</area_state>
<state_code>0</state_code>
</RECORD1>
</CFG0>
</fcm>

使用范例

         public override void OnInitialize()
{
XmlReader = new XmlReader ();
if ( cfg.Open( "areas" ) )
{
cfg.SeekConfig();
Int32 record_count = cfg.RecordCount;
for ( Int32 i = ; i < record_count; i++ )
{
if ( cfg.SeekNextRecord() )
{
Int32 area_id= cfg.RecordInt( "area_id" );
String area_name= cfg.RecordString( "area_name" );
}
}
}
else
{
String title = "No Exit Xml file : ";
title += TableConfig.XmlTitle( TableConfigType.Resource );
UnityEngine.Debug.LogWarning( "" + title );
}
cfg.Close();
}