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 ...
随机推荐
- vi命令模式下快速注释代码的方法
进入http://www.vim.org/scripts/script.php?script_id=1528 点击这个链接下载comments.vim这个插件 然后把它放入到./vim/plugin下 ...
- Linux内核分析之理解进程调度时机跟踪分析进程调度与进程切换的过程
一.原理分析 1.调度时机 背景不同类型的进程有不同的调度需求第一种分类I/O-bond:频繁的进行I/O:通常会花费很多时间等待I/O操作的完成CPU-bound:计算密集型:需要大量的CPU时间进 ...
- java轻量级Http Server
lighttpd 官方主页:www.lighttpd.netLighttpd是一个德国人领导的开源软件,其根本的目的是提供一个专门针对高性能网站,安全.快速.兼容性好并且灵活的web server环境 ...
- C++是一把很奇怪的刀
C++是一把很奇怪的刀,首尾都是刀刃.用刀能出什么,还是要看拿刀的人.
- 【C-数组】
一.一维数组 ①.定义方式 类型说明符 数组名 [常量表达式]; 如:int array[10]; 注意: 1) 数组的类型实际上是指数组元素的类型.对于同一个数组,其所有元素的数据类型都是相同的. ...
- Boost 安装
1.下载boost Windows版,如:boost_1_55_0: 2.运行boostrap.bat,会生成bjam.exe: 3.运行bjam.exe(时间会比较长),会生成一个stage目录里面 ...
- http://blog.sina.com.cn/s/blog_4c3b6a070100etad.html
http://blog.sina.com.cn/s/blog_4c3b6a070100etad.html
- rhel 5.8下静默安装oracle11gr2
1.图形界面下录制脚本如下: #-------------------------------------------------------------------------------# Do ...
- Linux之脚本安装软件
查看启动程序 ps aux 准备工作 1.保证yum源正常使用 2.关闭SELinux和防火墙 下载脚本文件包 解压缩 运行 ./centors.sh
- 解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译)
解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译) http://improve.dk/where-does-sql-server-store-the-sourc ...