C# read xml from a string and performance test
2008-02-07 19:40
441 查看
using System;
using System.Data;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string xml = @"<?xml version='1.0'?><person firstname='bill' lastname='gates' />";
string s = string.Empty;
DateTime start = DateTime.Now;
TimeSpan ts;
for (int i = 0; i < 3000; ++i)
s = TestInnerXML(xml);
//around 900 milliseconds
ts = DateTime.Now - start;
Console.WriteLine("TestInnerXML elapsed time = " +
ts.Milliseconds.ToString());
start = DateTime.Now;
for (int i = 0; i < 3000; ++i)
s = TestXMLReader(xml);
//around 90-100 milliseconds (9 times faster.)
ts = DateTime.Now - start;
Console.WriteLine("TestXMLReader elapsed time = " +
ts.Milliseconds.ToString());
Console.WriteLine("Press Enter to Continue ...");
Console.ReadLine();
}
public static string TestInnerXML(string xmlString)
{
XmlDocument doc = new XmlDocument();
doc.InnerXml = xmlString;
XmlElement root = doc.DocumentElement;
return root.GetAttribute("firstname");
}
public static string TestXMLReader(string xmlString)
{
XmlTextReader reader = new XmlTextReader(xmlString, XmlNodeType.Document, null);
reader.MoveToContent();
string s = reader.GetAttribute("firstname");
reader.Close();
return s;
}
}
}
using System.Data;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string xml = @"<?xml version='1.0'?><person firstname='bill' lastname='gates' />";
string s = string.Empty;
DateTime start = DateTime.Now;
TimeSpan ts;
for (int i = 0; i < 3000; ++i)
s = TestInnerXML(xml);
//around 900 milliseconds
ts = DateTime.Now - start;
Console.WriteLine("TestInnerXML elapsed time = " +
ts.Milliseconds.ToString());
start = DateTime.Now;
for (int i = 0; i < 3000; ++i)
s = TestXMLReader(xml);
//around 90-100 milliseconds (9 times faster.)
ts = DateTime.Now - start;
Console.WriteLine("TestXMLReader elapsed time = " +
ts.Milliseconds.ToString());
Console.WriteLine("Press Enter to Continue ...");
Console.ReadLine();
}
public static string TestInnerXML(string xmlString)
{
XmlDocument doc = new XmlDocument();
doc.InnerXml = xmlString;
XmlElement root = doc.DocumentElement;
return root.GetAttribute("firstname");
}
public static string TestXMLReader(string xmlString)
{
XmlTextReader reader = new XmlTextReader(xmlString, XmlNodeType.Document, null);
reader.MoveToContent();
string s = reader.GetAttribute("firstname");
reader.Close();
return s;
}
}
}
相关文章推荐
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- C# Serialize and Deserialize XML string
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- Call C# Code from C++ and read an array of struct which contains strings
- Using C# Yield for Readability and Performance
- C# Read and Modify XML File
- The XML Litmus Test Understanding When and Why to Use XML
- C# pdf 缩放纸型-iTextSharp - read an existing pdf, resize it and then draw it on a new one
- <From C++ To C#>非驴非马的string
- Save and Load from XML
- "IValueConverter" does not support converting from a string.——C# WPF 提示错误
- SSH error ( Read from socket failed: Connection reset by peer ) and it's solution
- String.Format in Java and C#
- XML and Java technologies: Data binding, Part 2: Performance
- Spring Batch Read from DB and Write to File
- 第3月第21天 nsclassfromstring返回null SVN报错:clean the working copy and then retry the operation
- Test-Driven C# Improve the Design and Flexibility of Your Project with Extreme Programming Techniques
- comparing c++ and c# -- a perspective from high level languages such as C#
- Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET