读取XML文件的几种方式的效率分析

时间:2021-08-05 12:36:25

第一种:使用XmlReader来读取。

  

 Stopwatch sw = Stopwatch.StartNew();
List<Dictionary<string, string>> entityInfo = new List<Dictionary<string, string>>();
using (XmlReader reader = new XmlTextReader(compareXmlName))
{
Dictionary<string, string> xmlValue = null;
string key = string.Empty;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (string.Compare(reader.LocalName, "MultiTable",
StringComparison.OrdinalIgnoreCase) == )
{
xmlValue = new Dictionary<string, string>();
}
else if(string.Compare(reader.LocalName,"NewDataSet",
StringComparison.OrdinalIgnoreCase) != )
{
key = reader.LocalName;
}
break;
case XmlNodeType.EndElement:
if (string.Compare(reader.LocalName, "MultiTable",
StringComparison.OrdinalIgnoreCase) == )
{
if (xmlValue != null)
{
entityInfo.Add(xmlValue);
xmlValue = null;
}
}
break;
case XmlNodeType.Text:
if (xmlValue != null)
{
xmlValue.Add(key, reader.Value);
}
break;
}
}
}
TimeSpan ts = sw.Elapsed; List<Customers> packData = new List<Customers>();
foreach (var item in entityInfo)
{
Customers temp = new Customers();
foreach (var sub in item)
{
if (sub.Key == "CustomerID")
{
temp.CustomerID = int.Parse(sub.Value);
}
else if (sub.Key == "CustomerName")
{
temp.CustomerName = sub.Value;
}
else if (sub.Key == "Description")
{
temp.Description = sub.Value;
}
else if (sub.Key == "Boolean")
{
temp.IsEnabled = sub.Value == "true" ? true : false;
}
}
packData.Add(temp);
} dataGridView3.DataSource = packData;
this.textBox9.Text = ts.TotalMilliseconds.ToString();

这种方式其实是最原始的读取方法,读取的时候一个一个的节点来读取,节点的类型有很多,比如Element类型,EndElement类型,Text类型等等,通过这种方式,我们来读取我们需要的数据。这种方式效率最高。

第二种:使用XmlDocument的方式来读取:

 Stopwatch sw = Stopwatch.StartNew();
XmlDocument doc = new XmlDocument();
doc.Load(compareXmlName);
XmlNode root = doc.DocumentElement;
XmlNode child = root.FirstChild;
List<Customers> results = new List<Customers>();
while (child != null)
{
XmlNode grandSon = child.FirstChild;
Customers temp = new Customers();
while (grandSon != null)
{
if (grandSon.Name == "CustomerID")
{
temp.CustomerID = int.Parse(grandSon.FirstChild.Value);
}
else if (grandSon.Name == "CustomerName")
{
temp.CustomerName = grandSon.FirstChild.Value;
}
else if (grandSon.Name == "Description")
{
temp.Description = grandSon.FirstChild.Value;
}
else if (grandSon.Name == "Boolean")
{
temp.IsEnabled = grandSon.FirstChild.Value == "true" ? true : false;
}
grandSon = grandSon.NextSibling;
}
results.Add(temp);
child = child.NextSibling;
}
TimeSpan ts = sw.Elapsed; dataGridView3.DataSource = results;
textBox10.Text = ts.TotalMilliseconds.ToString();

这种方式其实也是通过xmlLoader来构造节点的,xmlLoader里面也是XmlReader完成的任务,这种方式的效率也比较高。

第三种:直接使用DataSet来读取,自动判断Schema信息。

           DataSet compareDataSet1 = new DataSet("CompareDataSet");
Stopwatch sw = Stopwatch.StartNew();
//读取架构信息 compareDataSet1.ReadXml(compareXmlName, XmlReadMode.Auto); TimeSpan ts = sw.Elapsed;
this.dataGridView3.DataSource = compareDataSet1.Tables[];
textBox7.Text = ts.TotalMilliseconds.ToString();

  这种方式时效率最不高的,在调用ReadXml的时候,也是通过XmlReader来完成的工作,调用XmlLoader来加载数据,加载数据是通过XmlLoader的LoadTable来构造表的数据的。

第四种:也是使用DataSet来读取数据,但是先读取架构信息,并且调用BeginLoadData来关闭通知,索引维护等,这样的速度比第三种要快。

 DataSet compareDataSet1 = new DataSet("CompareDataSet");
Stopwatch sw = Stopwatch.StartNew();
//读取架构信息
compareDataSet1.ReadXmlSchema(compareXmlSchemaName);
//防止在修改数据的过程中引发各种约束的检查,并可以提高速度
foreach (DataTable dt in compareDataSet.Tables)
{
dt.BeginLoadData();
}
compareDataSet1.ReadXml(compareXmlName, XmlReadMode.IgnoreSchema);
foreach (DataTable dt in compareDataSet1.Tables)
{
dt.EndLoadData();
}
TimeSpan ts = sw.Elapsed;
this.dataGridView3.DataSource = compareDataSet1.Tables[];
textBox6.Text = ts.TotalMilliseconds.ToString();

第五种:也还是使用DataSet来读取数据,也读取架构信息,但是不调用BeginLoadData方法,在这里,因为没有添加任何的索引,约束等,所以速度和第四种差不多。

  DataSet compareDataSet1 = new DataSet("CompareDataSet");
Stopwatch sw = Stopwatch.StartNew();
//读取架构信息
compareDataSet1.ReadXmlSchema(compareXmlSchemaName); compareDataSet1.ReadXml(compareXmlName, XmlReadMode.IgnoreSchema); TimeSpan ts = sw.Elapsed;
this.dataGridView3.DataSource = compareDataSet1.Tables[];
textBox8.Text = ts.TotalMilliseconds.ToString();

五种的比较结果,读取10万条数据的结果如下:

读取XML文件的几种方式的效率分析

http://files.cnblogs.com/files/monkeyZhong/DataSetXML.zip