三种读取XML方法的比较.docx
文本预览下载声明
HYPERLINK /pengstone/archive/2012/08/08/2628953.html 使用XMLReader、XMLDocument和DataSet读取xml文件及效率
????? 在 .NET 开发中经常需要读取和操作XML文件,例如:操作配置文件(web.config和app.config)、读取业务设置的xml文件等。以前都喜欢用DataSet直接读取或写入xml,当文件小的时候,读取效率还能接受,但是当文件很大的时候,读取就变得很慢了。闲暇之于就对 XMLReader 、 XMLDocument和DataSet 读取XML文件进行简单总结,对效率进行简单比较。
1、XMLReader 读取XML文件
??????? XMLReader 提供对 XML 数据进行快速、非缓存、只进访问的读取器。XMLReader 只能读取xml文件,需要我们自己控制怎样获取相应的xml节点的信息,适合于读取很大的xml文件。
XMLReader 有一个类型为XmlNodeType的NodeType只读属性,通过它可以知道当前节点类型,以及根据节点类型和具体需求获取相应节点的信息。更详细的信息可以到微软技术资源库进行查询和了解。XMLReader读取XML文件方式如下:
1: static ListDictionarystring, string XMLReaderTest(string xmlPath)
2: {
3: ListDictionarystring, string entityInfo = new ListDictionarystring, string();
4: using (XmlReader reader = new XmlTextReader(xmlPath))
5: {
6: Dictionarystring, string xmlValue = null;
7: string key = string.Empty;
8: while (reader.Read())
9: {
10: switch (reader.NodeType)
11: {
12: case XmlNodeType.Element:
13: if (string.Compare(reader.LocalName, BE_WorkStation_ACInstance, StringComparison.OrdinalIgnoreCase) == 0)
14: {
15: xmlValue = new Dictionarystring, string();
16: }
17: else
18: {
19: if (string.Compare(reader.LocalName, EntitySchema, StringComparison.OrdinalIgnoreCase) != 0)
20: {
21: key = reader.LocalName;
22: }
23: }
24: break;
25: case XmlNodeType.EndElement:
26: if (string.Com
显示全部