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 ...
随机推荐
- 【dubbo】dubbo项目基本结构及provider构建
dubbo项目基本结构如下,分别部署于不同服务器: 1.provider(接口API 实现) 2.consumer(web) 3.zookeeper 4.DB provider构建 1.api构建 i ...
- 使用getopt()处理命令行参数
假设有一程序 testopt,其命令行选项参数有: -i 选项 -l 选项 -r 选项 -n <值> 带关联值的选项 则处理 ...
- 深入浅出C#中的静态与非静态
C#语言静态类 vs 普通类 C#语言静态类与普通类的区别有以下几点: 1)C#语言静态类无法实例化而普通类可以: 2)C#语言静态类只能从System.Object基类继承:普通可以继承其它任何非 ...
- cordova for ios(android一样)添加插件
1.进入当前工程文件夹 终端:cd ~/Desktop/ cd piao 2.添加插件 :cordova plugin add Basic device information (Device API ...
- Mysql运行SQL文件 错误Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
问题描述 想从服务器上DOWN下数据库.操作:先把数据库转存为SQL文件,然后在本地利用navicate运行SQL文件,出现错误信息: Incorrect table definition;there ...
- WPF,Silverlight与XAML读书笔记第四十五 - 外观效果之模板
说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. 模板允许用任何东西完全替换一个元素的可视树, ...
- ASP.NET MVC请求处理管道生命周期的19个关键环节(1-6)
ASP.NET和ASP.NET MVC的HttpApplication请求处理管道有共同的部分和不同之处,本系列将体验ASP.NET MVC请求处理管道生命周期的19个关键环节. ①以IIS6.0为例 ...
- 移动App开发需要更多的PaaS平台而不是IaaS
时代的变迁,创业的大潮,越来越多的人关注了有点开发,越来越多的人了解了互联网服务术语:PaaS.IaaS.SaaS.BaaS等.今天大家在开发App的时候这么多复杂的云服务如何来选择呢? IaaS服务 ...
- 作业七:团队项目——Alpha版本冲刺阶段-08
昨天进展:代码编写. 今天安排:代码编写.
- C# 获取当前日期在指定日期范围内是第几周
public static int GetWeekOfDay(DateTime start, DateTime end) { //总周数 )); //用于存储日期 var weekDic = new ...