第一种:使用XmlReader来读取。
1 Stopwatch sw = Stopwatch.StartNew(); 2 List<Dictionary<string, string>> entityInfo = new List<Dictionary<string, string>>(); 3 using (XmlReader reader = new XmlTextReader(compareXmlName)) 4 { 5 Dictionary<string, string> xmlValue = null; 6 string key = string.Empty; 7 while (reader.Read()) 8 { 9 switch (reader.NodeType) 10 { 11 case XmlNodeType.Element: 12 if (string.Compare(reader.LocalName, "MultiTable", 13 StringComparison.OrdinalIgnoreCase) == 0) 14 { 15 xmlValue = new Dictionary<string, string>(); 16 } 17 else if(string.Compare(reader.LocalName,"NewDataSet", 18 StringComparison.OrdinalIgnoreCase) != 0) 19 { 20 key = reader.LocalName; 21 } 22 break; 23 case XmlNodeType.EndElement: 24 if (string.Compare(reader.LocalName, "MultiTable", 25 StringComparison.OrdinalIgnoreCase) == 0) 26 { 27 if (xmlValue != null) 28 { 29 entityInfo.Add(xmlValue); 30 xmlValue = null; 31 } 32 } 33 break; 34 case XmlNodeType.Text: 35 if (xmlValue != null) 36 { 37 xmlValue.Add(key, reader.Value); 38 } 39 break; 40 } 41 } 42 } 43 TimeSpan ts = sw.Elapsed; 44 45 List<Customers> packData = new List<Customers>(); 46 foreach (var item in entityInfo) 47 { 48 Customers temp = new Customers(); 49 foreach (var sub in item) 50 { 51 if (sub.Key == "CustomerID") 52 { 53 temp.CustomerID = int.Parse(sub.Value); 54 } 55 else if (sub.Key == "CustomerName") 56 { 57 temp.CustomerName = sub.Value; 58 } 59 else if (sub.Key == "Description") 60 { 61 temp.Description = sub.Value; 62 } 63 else if (sub.Key == "Boolean") 64 { 65 temp.IsEnabled = sub.Value == "true" ? true : false; 66 } 67 } 68 packData.Add(temp); 69 } 70 71 dataGridView3.DataSource = packData; 72 this.textBox9.Text = ts.TotalMilliseconds.ToString();
这种方式其实是最原始的读取方法,读取的时候一个一个的节点来读取,节点的类型有很多,比如Element类型,EndElement类型,Text类型等等,通过这种方式,我们来读取我们需要的数据。这种方式效率最高。
第二种:使用XmlDocument的方式来读取:
1 Stopwatch sw = Stopwatch.StartNew(); 2 XmlDocument doc = new XmlDocument(); 3 doc.Load(compareXmlName); 4 XmlNode root = doc.DocumentElement; 5 XmlNode child = root.FirstChild; 6 List<Customers> results = new List<Customers>(); 7 while (child != null) 8 { 9 XmlNode grandSon = child.FirstChild; 10 Customers temp = new Customers(); 11 while (grandSon != null) 12 { 13 if (grandSon.Name == "CustomerID") 14 { 15 temp.CustomerID = int.Parse(grandSon.FirstChild.Value); 16 } 17 else if (grandSon.Name == "CustomerName") 18 { 19 temp.CustomerName = grandSon.FirstChild.Value; 20 } 21 else if (grandSon.Name == "Description") 22 { 23 temp.Description = grandSon.FirstChild.Value; 24 } 25 else if (grandSon.Name == "Boolean") 26 { 27 temp.IsEnabled = grandSon.FirstChild.Value == "true" ? true : false; 28 } 29 grandSon = grandSon.NextSibling; 30 } 31 results.Add(temp); 32 child = child.NextSibling; 33 } 34 TimeSpan ts = sw.Elapsed; 35 36 dataGridView3.DataSource = results; 37 textBox10.Text = ts.TotalMilliseconds.ToString();
这种方式其实也是通过xmlLoader来构造节点的,xmlLoader里面也是XmlReader完成的任务,这种方式的效率也比较高。
第三种:直接使用DataSet来读取,自动判断Schema信息。
1 DataSet compareDataSet1 = new DataSet("CompareDataSet"); 2 Stopwatch sw = Stopwatch.StartNew(); 3 //读取架构信息 4 5 compareDataSet1.ReadXml(compareXmlName, XmlReadMode.Auto); 6 7 TimeSpan ts = sw.Elapsed; 8 this.dataGridView3.DataSource = compareDataSet1.Tables[0]; 9 textBox7.Text = ts.TotalMilliseconds.ToString();
这种方式时效率最不高的,在调用ReadXml的时候,也是通过XmlReader来完成的工作,调用XmlLoader来加载数据,加载数据是通过XmlLoader的LoadTable来构造表的数据的。
第四种:也是使用DataSet来读取数据,但是先读取架构信息,并且调用BeginLoadData来关闭通知,索引维护等,这样的速度比第三种要快。
1 DataSet compareDataSet1 = new DataSet("CompareDataSet"); 2 Stopwatch sw = Stopwatch.StartNew(); 3 //读取架构信息 4 compareDataSet1.ReadXmlSchema(compareXmlSchemaName); 5 //防止在修改数据的过程中引发各种约束的检查,并可以提高速度 6 foreach (DataTable dt in compareDataSet.Tables) 7 { 8 dt.BeginLoadData(); 9 } 10 compareDataSet1.ReadXml(compareXmlName, XmlReadMode.IgnoreSchema); 11 foreach (DataTable dt in compareDataSet1.Tables) 12 { 13 dt.EndLoadData(); 14 } 15 TimeSpan ts = sw.Elapsed; 16 this.dataGridView3.DataSource = compareDataSet1.Tables[0]; 17 textBox6.Text = ts.TotalMilliseconds.ToString();
第五种:也还是使用DataSet来读取数据,也读取架构信息,但是不调用BeginLoadData方法,在这里,因为没有添加任何的索引,约束等,所以速度和第四种差不多。
1 DataSet compareDataSet1 = new DataSet("CompareDataSet"); 2 Stopwatch sw = Stopwatch.StartNew(); 3 //读取架构信息 4 compareDataSet1.ReadXmlSchema(compareXmlSchemaName); 5 6 compareDataSet1.ReadXml(compareXmlName, XmlReadMode.IgnoreSchema); 7 8 TimeSpan ts = sw.Elapsed; 9 this.dataGridView3.DataSource = compareDataSet1.Tables[0]; 10 textBox8.Text = ts.TotalMilliseconds.ToString();
五种的比较结果,读取10万条数据的结果如下:
http://files.cnblogs.com/files/monkeyZhong/DataSetXML.zip
时间: 2024-10-09 01:11:46