C# XML读取器性能

C# XML读取器性能,c#,asp.net,xml,xmlreader,xmltextreader,C#,Asp.net,Xml,Xmlreader,Xmltextreader,我已经将我的应用程序中的惰性追溯到下面计时的代码位。我知道这将是一个缓慢的点,但每个请求平均需要1秒。我所追求的xml总是在第一个标记中,所以我不认为是下载时间影响了我 Stopwatch stopwatch = new Stopwatch(); XmlTextReader reader = new XmlTextReader("http://steamcommunity.com/id/test?xml=1"); stopwatch.Reset(); stopwatch.Start(); w

我已经将我的应用程序中的惰性追溯到下面计时的代码位。我知道这将是一个缓慢的点,但每个请求平均需要1秒。我所追求的xml总是在第一个标记中,所以我不认为是下载时间影响了我

Stopwatch stopwatch = new Stopwatch();
XmlTextReader reader = new XmlTextReader("http://steamcommunity.com/id/test?xml=1");

stopwatch.Reset();
stopwatch.Start();

while (reader.Read()) {
 if (reader.Name.Equals("steamID64")) {
  reader.Read();

  stopwatch.Stop();

  time = stopwatch.ElapsedMilliseconds();
  return Convert.ToInt64(reader.Value);
 }
}
是否有一种更快的方法来读取我想要的标记,或者我是否受到从中下载xml文件的服务器的限制


谢谢。

我认为您正在测量创建连接所需的时间。要确认这一点,您可以将Reset+Start行上移到读卡器创建上方。我预计两者之间不会有什么区别


如果是连接时间,则取决于网络,并且代码中没有可以执行的操作。也许你可以从网络设置中得到一些改进。但这是另一个论坛。

我认为您正在测量创建连接所需的时间。要确认这一点,您可以将Reset+Start行上移到读卡器创建上方。我预计两者之间不会有什么区别

如果是连接时间,则取决于网络,并且代码中没有可以执行的操作。也许你可以从网络设置中得到一些改进。但这是另一个论坛。

尝试设置

reader.XmlResolver = null;
尝试设置

reader.XmlResolver = null;
所以我不认为是下载时间让我抓狂

为了确认这一点,您是否考虑过在本地下载该文件,然后看看时间是什么样的

所以我不认为是下载时间让我抓狂


为了确认这一点,您是否考虑过在本地下载文件,然后看看时间是什么样的,我将您的方法与另一种方法进行了比较。只需下载数据并通过正则表达式查找id

        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Reset();
        stopwatch.Start();

        System.Net.WebClient wc = new System.Net.WebClient();
        string s = wc.DownloadString("http://steamcommunity.com/id/test?xml=1");
        System.Text.RegularExpressions.Regex re = new Regex("\\<steamID64\\>(\\d+)\\</steamID64\\>");
        System.Text.RegularExpressions.Match m = re.Match(s);
        if (m != null && m.Captures.Count != 0) Response.Write("steamID64: " + m.Captures[0].Value + " <br/>");
        stopwatch.Stop();

        long time = stopwatch.ElapsedMilliseconds;
        Response.Write("Time Elapsed (1):" + time.ToString() +" <br/>");

        stopwatch.Reset();
        stopwatch.Start();

        System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("http://steamcommunity.com/id/test?xml=1");

        stopwatch.Reset();
        stopwatch.Start();

        while (reader.Read())
        {
            if (reader.Name.Equals("steamID64"))
            {
                reader.Read();
                stopwatch.Stop();

                time = stopwatch.ElapsedMilliseconds;
                s = reader.Value;
                break;
            }
        }

        Response.Write("<br/>steamID64: " + s );
        Response.Write("<br/>Time Elapsed (2):" + time.ToString() + " <br/>");
System.Diagnostics.Stopwatch秒表=新的System.Diagnostics.Stopwatch();
秒表复位();
秒表。开始();
System.Net.WebClient wc=新系统.Net.WebClient();
字符串s=wc.DownloadString(“http://steamcommunity.com/id/test?xml=1");
System.Text.RegularExpressions.Regex re=new Regex(“\\(\\d+\”);
System.Text.RegularExpressions.Match m=重新匹配;
如果(m!=null&&m.Captures.Count!=0)Response.Write(“streamid64:+m.Captures[0].Value+”
); 秒表; 长时间=秒表。延时毫秒; Write(“经过的时间(1):“+Time.ToString()+”
); 秒表复位(); 秒表。开始(); System.Xml.XmlTextReader=新System.Xml.XmlTextReader(“http://steamcommunity.com/id/test?xml=1"); 秒表复位(); 秒表。开始(); while(reader.Read()) { if(reader.Name.Equals(“steamID64”)) { reader.Read(); 秒表; 时间=秒表。延时毫秒; s=读取器值; 打破 } } 响应。写入(“
streamid64:+s”); Write(“
经过的时间(2):“+Time.ToString()+”
”);
**结果:

steamID64:76561197991558078 所用时间(1):1572

steamID64:76561197991558078 经过的时间(2):969


XmlReader更好:)。

我将您的方法与另一种方法进行了比较。只需下载数据并通过正则表达式查找id

        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Reset();
        stopwatch.Start();

        System.Net.WebClient wc = new System.Net.WebClient();
        string s = wc.DownloadString("http://steamcommunity.com/id/test?xml=1");
        System.Text.RegularExpressions.Regex re = new Regex("\\<steamID64\\>(\\d+)\\</steamID64\\>");
        System.Text.RegularExpressions.Match m = re.Match(s);
        if (m != null && m.Captures.Count != 0) Response.Write("steamID64: " + m.Captures[0].Value + " <br/>");
        stopwatch.Stop();

        long time = stopwatch.ElapsedMilliseconds;
        Response.Write("Time Elapsed (1):" + time.ToString() +" <br/>");

        stopwatch.Reset();
        stopwatch.Start();

        System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("http://steamcommunity.com/id/test?xml=1");

        stopwatch.Reset();
        stopwatch.Start();

        while (reader.Read())
        {
            if (reader.Name.Equals("steamID64"))
            {
                reader.Read();
                stopwatch.Stop();

                time = stopwatch.ElapsedMilliseconds;
                s = reader.Value;
                break;
            }
        }

        Response.Write("<br/>steamID64: " + s );
        Response.Write("<br/>Time Elapsed (2):" + time.ToString() + " <br/>");
System.Diagnostics.Stopwatch秒表=新的System.Diagnostics.Stopwatch();
秒表复位();
秒表。开始();
System.Net.WebClient wc=新系统.Net.WebClient();
字符串s=wc.DownloadString(“http://steamcommunity.com/id/test?xml=1");
System.Text.RegularExpressions.Regex re=new Regex(“\\(\\d+\”);
System.Text.RegularExpressions.Match m=重新匹配;
如果(m!=null&&m.Captures.Count!=0)Response.Write(“streamid64:+m.Captures[0].Value+”
); 秒表; 长时间=秒表。延时毫秒; Write(“经过的时间(1):“+Time.ToString()+”
); 秒表复位(); 秒表。开始(); System.Xml.XmlTextReader=新System.Xml.XmlTextReader(“http://steamcommunity.com/id/test?xml=1"); 秒表复位(); 秒表。开始(); while(reader.Read()) { if(reader.Name.Equals(“steamID64”)) { reader.Read(); 秒表; 时间=秒表。延时毫秒; s=读取器值; 打破 } } 响应。写入(“
streamid64:+s”); Write(“
经过的时间(2):“+Time.ToString()+”
”);
**结果:

steamID64:76561197991558078 所用时间(1):1572

steamID64:76561197991558078 经过的时间(2):969


XmlReader更好:)。

试试XPathNavigator对象-XmlTextReader是一个一次性对象。@Yuriy,你的意思是使用reader.close()?我正在这样做,只是没有把它包含在上面的代码片段中。我想Yuriy的意思是用
using
来包装声明,就像这样:
using(XmlTextReader=new XmlTextReader(“http://steamcommunity.com/id/test?xml=1){…}
。这样会自动处理它(也在发生异常时):)尝试XPathNavigator对象-XmlTextReader是一次性对象。@Yuriy,您的意思是使用reader.close()?我正在这样做,只是没有把它包含在上面的代码片段中。我想Yuriy的意思是用
using
来包装声明,就像这样:
using(XmlTextReader=new XmlTextReader(“http://steamcommunity.com/id/test?xml=1){…}
。通过这种方式,它被自动处理(也在发生异常的情况下):)似乎没有帮助,尽管我可能需要做很多迭代来确定。似乎没有帮助,尽管我可能需要做很多迭代