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 ...
随机推荐
- JS变量和函数的一些理解
今日看了下JS变量的一些文章,有些感触,把自己总结的一些写出来. JS初始化的过程1.JS解释器执行代码之前,创建全局变量2.用预定义的值和函数来初始化全局对象中的属性,3.搜索函数外的var声明,创 ...
- CXF实现webservice
虽然网上有很多cxf的教程,但还是要自己写写, “好记性不如烂笔头” 1.服务端 1.1 DEMO,用于测试传递对象 package com.xq.model; import javax.persi ...
- hdu 5720
考虑三个树枝:a,b,c若c是将要抛出的树枝,那么形成三角形的条件是a+b>c and a-b<c 可以写成 c属于开区间(a-b,a+b)对于每个C和许许多多的其他边,如何保证C不构成三 ...
- [1008]harder_prime
素数定义:一个大于1的整数,如果它的约数如果只有1和它本身,那么它就是一个素数. 回文数定义:一个整数把它的各位数字倒过来还是它本身,那么它就是回文数,比如说2,99,393. 回文素数定义:一个数如 ...
- 2.使用JDK开发webService
使用jdk开发webService需要注意:jdk版本必须1.6以及1.6以上! 以下webService的组成部分: server端和client端,通过服务器端(server)webService ...
- WLAN信道
- Oracle EBS进化史
https://blogs.oracle.com/ptian/entry/oracle_ebs%E8%BF%9B%E5%8C%96%E5%8F%B2 通过图表总结了下Oracle EBS的进化历史,回 ...
- Oracle Created Database Users: Password, Usage and Files References (文档 ID 160861.1)
This document is no longer actively maintained, for info on specific (new) users in recent product e ...
- asp.net WebApi and protobuff
protobuff 是谷歌开发的,在性能上要比Json xml好很多,对性能要求比较高的时候这个是一个不错的选择,但是这个目前只是一个序列化反序列化的东西,以前原生的只有几种语言的现在在github ...
- 让ZenCoding提升编码速度
日前写了一篇关于VS神级插件Web Essentials的系列博客,其中在HTML&CSS操作技巧一节简单提到了ZenCoding,今天来详细说一下这个东西. 摘要 Zen Coding是一种 ...