c# - 在 C# 中解析 XML 文件的最快方法?

标签 c# sql xml c#-4.0

我必须从 Internet 加载许多 XML 文件。但是为了以更快的速度进行测试,我下载了以下格式的所有文件(超过 500 个文件)。

<player-profile>
  <personal-information>
    <id>36</id>
    <fullname>Adam Gilchrist</fullname>
    <majorteam>Australia</majorteam>
    <nickname>Gilchrist</nickname>
    <shortName>A Gilchrist</shortName>
    <dateofbirth>Nov 14, 1971</dateofbirth>
    <battingstyle>Left-hand bat</battingstyle>
    <bowlingstyle>Right-arm offbreak</bowlingstyle>
    <role>Wicket-Keeper</role>
    <teams-played-for>Western Australia, New South Wales, ICC World XI, Deccan Chargers, Australia</teams-played-for>
    <iplteam>Deccan Chargers</iplteam>
  </personal-information>
  <batting-statistics>
    <odi-stats>
      <matchtype>ODI</matchtype>
      <matches>287</matches>
      <innings>279</innings>
      <notouts>11</notouts>
      <runsscored>9619</runsscored>
      <highestscore>172</highestscore>
      <ballstaken>9922</ballstaken>
      <sixes>149</sixes>
      <fours>1000+</fours>
      <ducks>0</ducks>
      <fifties>55</fifties>
      <catches>417</catches>
      <stumpings>55</stumpings>
      <hundreds>16</hundreds>
      <strikerate>96.95</strikerate>
      <average>35.89</average>
    </odi-stats>
    <test-stats>
      .
      .
      .
    </test-stats>
    <t20-stats>
      .
      .
      .    
    </t20-stats>
    <ipl-stats>
      .
      .
      . 
    </ipl-stats>
  </batting-statistics>
  <bowling-statistics>
    <odi-stats>
      <matchtype>ODI</matchtype>
      <matches>378</matches>
      <ballsbowled>58</ballsbowled>
      <runsgiven>64</runsgiven>
      <wickets>3</wickets>
      <fourwicket>0</fourwicket>
      <fivewicket>0</fivewicket>
      <strikerate>19.33</strikerate>
      <economyrate>6.62</economyrate>
      <average>21.33</average>
    </odi-stats>
    <test-stats>
      .
      .
      . 
    </test-stats>
    <t20-stats>
      .
      .
      . 
    </t20-stats>
    <ipl-stats>
      .
      .
      . 
    </ipl-stats>
  </bowling-statistics>
</player-profile>

我正在使用

XmlNodeList list = _document.SelectNodes("/player-profile/batting-statistics/odi-stats");

然后用 foreach 作为

循环这个列表
foreach (XmlNode stats in list)
  {
     _btMatchType = GetInnerString(stats, "matchtype"); //it returns null string if node not availible
     .
     .
     .
     .
     _btAvg = Convert.ToDouble(stats["average"].InnerText);
  }

即使我离线加载所有文件,解析也很慢 有什么好的更快的方法来解析它们吗?还是 SQL 有问题?我使用 DataSets、TableAdapters 和插入命令将所有从 XML 中提取的数据保存到数据库中。

编辑: 现在要使用 XmlReader,请为上述文档提供一些 XmlReader 代码。现在,我已经做到了这一点

void Load(string url) 
{
    _reader = XmlReader.Create(url); 
    while (_reader.Read()) 
    { 
    } 
} 

XmlReader 的可用方法令人困惑。我需要的是完整地获取击球和保龄球统计数据,击球和保龄球统计数据是不同的,而保龄球和击球中的 odi、t2o、ipl 等是相同的。

最佳答案

您可以使用 XmlReader仅供快速阅读。

关于c# - 在 C# 中解析 XML 文件的最快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3046505/

相关文章:

c# - 发送电子邮件 asp.net c#

c# - Entity Framework 预加载 - 将数据传递给 ViewModel

c# - 我们如何检测 COM Interop 应用程序中的内存泄漏?

php - 使用 MySql 和 PHP 创建论坛网站时出现问题。不确定如何显示主题

c# - Microsoft 图表控件 : how to set chart width to width of surrounding div?

sql - 如何在 SQL Server 中保持数据行内

java - 使用 eXist-db 进行不区分大小写的搜索

java - 针对 xsd 的 Xml 验证接受错误的 xml 为有效

ios - iPhone - 如何向服务器发布和获取 xml 数据?

mysql - 1615 准备好的语句需要在 codeigniter 中重新准备