使用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:staticListDictionarystring,stringXMLReaderTest(stringxmlPath)2:{3:ListDictionarystring,stringentityInfo=newListDictionarystring,string();4:using(XmlReaderreader=newXmlTextReader(xmlPath))5:{6:Dictionarystring,stringxmlValue=null;7:stringkey=string.Empty;8:while(reader.Read())9:{10:switch(reader.NodeType)11:{12:caseXmlNodeType.Element:13:if(string.Compare(reader.LocalName,BE_WorkStation_ACInstance,StringComparison.OrdinalIgnoreCase)==0)14:{15:xmlValue=newDictionarystring,string();16:}17:else18:{19:if(string.Compare(reader.LocalName,EntitySchema,StringComparison.OrdinalIgnoreCase)!=0)20:{21:key=reader.LocalName;22:}23:}24:break;25:caseXmlNodeType.EndElement:26:if(string.Compare(reader.LocalName,BE_WorkStation_ACInstance,StringComparison.OrdinalIgnoreCase)==0)27:{28:if(xmlValue!=null)29:{30:entityInfo.Add(xmlValue);31:xmlValue=null;32:}33:}34:break;35:caseXmlNodeType.Text:36:if(xmlValue!=null)37:{38:xmlValue.Add(key,reader.Value);39:}40:break;41:default:42:break;43:}44:}45:}46:returnentityInfo;47:}2、XMLDocument读取XML文件XMLDocument表示XML文档在内存中的树形结构,它提供像js操作html文档一样的方式操作XML文档。在读取单个小XML文件时效率比较高。XMLDocument读取XML文件方式如下:1:staticListDictionarystring,stringXMLDocumentTest(stringxmlPath)2:{3:ListDictionarystring,stringentityInfo=newListDictionarystring,string();4:using(XmlReaderreader=newXmlTextReader(xmlPath))5:{6:XmlDocumentdoc=newXmlDocument();7:doc.Load(reader);8:XmlNodeListnodeList=doc.ChildNodes;9:foreach(XmlNodenodeinnodeList)10:{11:varxmlValue=newDictionarystring,string();12:foreach(XmlNodechildinnode.ChildNodes)13:{14:xmlValue[child.LocalName]=child.InnerText;15:}16:entityInfo.Add(xmlValue);17:}18:}19:returnentityInfo;20:}3、DataSet读取XML文件DataSet类型提供了一个ReadXml方法,它将XML架构和数据读入DataSet中。DataSet在读取XML文件时效率很低。DataSet读取XML文件方式如下:1:staticListDictionarystring,stringDataSetTest(stringxmlPath)2:{3:ListDictionarystring,stringentityInfo=newListDictionarystring,string();4:DataSetds=newDataSet();5://读取XML文件架构6:using(XmlReaderreader=newXmlTextReader(xmlPath))7:{8:ds.ReadXmlSchema(reader);9:}10:foreach(DataTabledtinds.Tables)11:{12:dt.BeginLoadData();13:}14:using(XmlReaderreader=newXmlTextReader(xmlPath))15:{16:ds.ReadXml(reader);17:}18:foreach(DataTabledtinds.Tables)19:{20:dt.EndLoadData();21:}22:if(ds.Tables.Count0)23:{24:DataTabledt=ds.Tables[0];25:foreach(DataRowrowindt.Rows)26:{27:varxmlValue=newDictionarystring,string();28:foreach(DataColumncolindt.Columns)29:{30:xmlValue[col.ColumnName]=row.Fieldstring(col);31:}32:entityInfo.Add(xmlValue);33:}34:}35:returnentityInfo;36:}读取的xml文件片段:1:?xmlversion=1.0encoding=utf-8?2:EntitySchema3:4:BE_WorkStation_ACInstance5:FieldNameMS_ACInstanceOID/FieldName6:FieldChinese主键/FieldChinese7:FieldType45/FieldType8:FieldLength500/FieldLength9:DecLength0/DecLength10:CodeTable选择/CodeTable11:fUseCodeTablefalse/fUseCodeTable12:fDisplaytrue/fDisplay13:DefaultValue/DefaultValue14:FieldKind0/FieldKind15:fCanModifytrue/fCanModify16:ForeignKeyField/ForeignKeyField17:LookupKeyField/LookupKeyField18:LookupDataSet/LookupDataSet19:LookupResultField选择列/LookupResultField20:fForeignKeyfalse/fForeignKey21:SqlColumnMS_ACInstance.MS_ACInstanceOIDasMS_ACInstanceOID/SqlColumn22:ifSubmittrue/ifSubmit23:SubmitColumnName/SubmitColumnName24:ifReadDatatrue/ifReadData25:ifChangeDataSettrue/ifChangeDataSet26:ColumnNumRule/ColumnNumRule27:EditorBusinessElement/EditorBusinessElement28:InputRule/InputRule29:InputRuleHelper/InputRuleHelper30:ifCheckInputNullfalse/ifCheckInputNull31:EncryptRule/EncryptRule32:/BE_WorkStation_ACInstance33:34:BE_WorkStation_ACInstance35:FieldNamesys_rank/FieldName36:FieldChinese排序标识/FieldChinese37:FieldType2/FieldType38:FieldLength500/FieldLength39:DecLength0/DecLength40:CodeTable选择/CodeTable41:fUseCodeTablefalse/fUseCodeTable42:fDisplaytrue/fDisplay43:DefaultValue/DefaultValue44:FieldKind0/FieldKind45:fCanModifytrue/fCanModify46:ForeignKeyField/ForeignKeyField47:LookupKeyField/LookupKeyField48:LookupDataSet/LookupDataSet49:LookupResultField选择列/LookupResultField50:fForeignKeyfalse/fForeignKey51:SqlColumnMS_ACInstance.BASE_Rankassys_rank/SqlColumn52:ifSubmittrue/ifSubmit53:SubmitColumnNameBASE_Rank/SubmitColumnName54:ifReadDatatrue/ifReadData55:ifChangeDataSettrue/ifChangeDataSet56:ColumnNumRule/ColumnNumRule57:EditorBusinessElement/EditorBusinessElement58:InputRule/InputRule59:InputRu