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();
}
C# XmlReader的更多相关文章
- XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate
namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...
- XmlReader和XElement组合之读取大型xml文档
简介 在.NET framework 中存在大量操作xml数据的类库和api,但在.NET framework 3.5后我们的首选一般就是linq to xml. linq to xml操作xml数据 ...
- php xml 文件读取 XMLReader
php xml 文件读取 <?php /** $xmlString = '<xml> <persons count="10"> <person ...
- XmlReader读取XML
StringBuilder output = new StringBuilder(); String xmlString = @"<bookstore> <book gen ...
- XML参考 :XmlReader 详解、实例
XML参考 :XmlReader 详解.实例-- 详解 转:http://www.cnblogs.com/Dlonghow/archive/2008/07/28/1252191.html XML参考 ...
- XmlWriter/XmlReader示例代码
在Silverlight项目中,如果您想最大程度的减少xap包的大小,仅使用默认System.Xml命名空间下提供的功能来实现“XML序列化/反序列化”,恐怕XmlReader/XmlWriter将成 ...
- C# XmlReader/XmlWriter 类
XmlReader用于读取Xml文件,XmlWriter用于将数据写到Xml文件.其实,在印象当中,XML很多的操作类都支持直接Save.Read也支持接受XmlReader与XmlWriter类的示 ...
- 使用XmlReader读取xml文件之二
在.net开发中经常需要读写xml形式的文件(app.config和web.config分别是WinForm和WebForm中使用到的 xml文件的一个特列,并且微软提供了通用的方法,在此就不赘述了) ...
- 使用XmlReader读Xml
XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...
随机推荐
- maven环境终于可以了
说说maven可以后小小的体会吧,虽然还没有用maven运行过工程,体会是pom.xml中的dependency属性可以帮助管理项目中的jar包,只要在这里配置下需要的jar包,保存后就会自动从中央仓 ...
- yuecheng 笑话
http://115.28.189.219:9898/stock/manager_articles/fundamentals 要闻 http://115.28.189.219:9898/stock/m ...
- 黑马程序员+ADO.Net基础(下)
---------------<a href="http://edu.csdn.net"target="blank">ASP.Net+Android ...
- 跟visual studio 集成的git插件
目前有三个,git extension,微软的 visual studio tools for git extension,还有git source control provider 经测试,最好用的 ...
- JAVA缓存技术
介绍 JNotify:http://jnotify.sourceforge.net/,通过JNI技术,让Java代码可以实时的监控制定文件夹内文件的变动信息,支持Linux/Windows/MacOS ...
- WTFPL 开源协议
中文翻译: 你他妈的随便公共许可 版本2, 2004年12月 版权所有(C) 2004 Sam Hocevar <sam@hocevar.net> 每个人都允许复制和散布或修改本授权文件的 ...
- crtmpserver系列(二):搭建简易流媒体直播系统
crtmpserver简介 我们在第一章的时候已经简要说明了crtmpserver,crtmpserver是一个由C++语言编写的开源的RTMP流媒体服务器,与其对应的商业产品自然是Adobe公司的F ...
- HTML5之Canvas时钟(网页效果--每日一更)
今天,带来的是使用HTML5中Canvas标签实现的动态时钟效果. 话不多说,先看效果:亲,请点击这里 众所周知,Canvas标签是HTML5中的灵魂,HTML5 Canvas是屏幕上的一个由Java ...
- 作业二:在github上过程
注册Github
- Asp.Net MVC中使用ACE模板之Jqgrid
第一次看到ACE模板,有种感动,有种相见恨晚的感觉,于是迅速来研究.它本身是基于bootstrap和jqueryui,但更nice,整合之后为后台开发节省了大量时间. 发现虽然不是完美,整体效果还是不 ...