【发布时间】:2011-03-04 01:19:43
【问题描述】:
我必须从 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 和 insert 命令将所有提取的数据从 XML 保存到数据库。
编辑: 现在要使用 XmlReader,请为上述文档提供一些 XmlReader 代码。现在,我已经这样做了
void Load(string url)
{
_reader = XmlReader.Create(url);
while (_reader.Read())
{
}
}
XmlReader 的可用方法令人困惑。我需要的是完整地获得击球和保龄球的统计数据,击球和保龄球的统计数据是不同的,而保龄球和击球的 odi、t2o、ipl 等是相同的。
【问题讨论】:
-
您是否尝试过使用 LINQ to XML 进行解析?
-
不,我没有,这是最好的方法吗?
-
您可能需要分离关注点(文件加载、节点提取、数据库交互等)来确定瓶颈。有几种方法可以提高每个组件的性能。
-
我是学生,这项工作是我项目的一部分。现在,我已经创建了一个类来解析所有这些 XML 文件并将它们从该类中保存到数据库中。是的,如果我可以拆分组件会更好,但我不知道如何,你能给我一个链接来学习吗?