如何在C#中通过API访问和处理XML

5

我的目标是从API中提取XML数据并将其加载到SQL服务器数据库中。我正在尝试的第一步是访问数据并显示它。一旦我成功,我将循环遍历每一行并将值插入到SQL服务器数据库中。当我尝试运行下面的代码时,什么也不会发生,当我直接将URL粘贴到浏览器中时,会出现以下错误:

"2010-03-08 04:24:17 Wallet exhausted: retry after 2010-03-08 05:23:58. 2010-03-08 05:23:58"

在我的看法中,foreach循环的每一次迭代都会对该网站进行调用,导致我被封禁了一个小时。我是以错误的方式从API检索数据吗?是否有一种方法可以将数据加载到内存或数组中,然后遍历它?

这是我拼凑出来的代码片段。

using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string userID = "123";
            string apiKey = "abc456";
            string characterID = "789";
            string url = "http://api.eve-online.com/char/WalletTransactions.xml.aspx?userID=" + userID + "&apiKey=" + apiKey + "&characterID=" + characterID;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(url);
            XmlNamespaceManager xnm1 = new XmlNamespaceManager(xmldoc.NameTable);
            XmlNodeList nList1 = xmldoc.SelectNodes("result/rowset/row", xnm1);
            foreach (XmlNode xNode in nList1)
            {
                Response.Write(xNode.InnerXml + "<br />");
            }
        }

        catch (SqlException em)
        {
            Response.Write(em.Message);
        }
    }
}

这是一个 XML 的示例。
<eveapi version="2"> 
  <currentTime>2010-03-06 17:38:35</currentTime> 
  <result> 
    <rowset name="transactions" key="transactionID" columns="transactionDateTime,transactionID,quantity,typeName,typeID,price,clientID,clientName,stationID,stationName,transactionType,transactionFor"> 
      <row transactionDateTime="2010-03-06 17:16:00" transactionID="1343566007" quantity="1" typeName="Co-Processor II" typeID="3888" price="1122999.00" clientID="1404318579" clientName="unseenstrike" stationID="60011572" stationName="Osmeden IX - Moon 6 - University of Caille School" transactionType="sell" transactionFor="personal" /> 
      <row transactionDateTime="2010-03-06 17:15:00" transactionID="1343565894" quantity="1" typeName="Co-Processor II" typeID="3888" price="1150000.00" clientID="1404318579" clientName="unseenstrike" stationID="60011572" stationName="Osmeden IX - Moon 6 - University of Caille School" transactionType="sell" transactionFor="personal" /> 
    </rowset> 
  </result> 
  <cachedUntil>2010-03-06 17:53:35</cachedUntil> 
</eveapi>

3
这个问题涉及特定的API。只有了解这个特定API的人才能告诉你为什么它会有那样的行为。 - John Saunders
1
Eve确实有一个开发者论坛,我也会在那里发布这个问题,但我觉得这更像是一个关于如何正确处理XML数据的一般性编程问题。 - Jerry
2个回答

4

通过快速搜索(使用谷歌)可以发现这是EVE API缓存机制的作用。如果你从相同的键和IP查询相同的数据,它会告诉你重复使用已有的数据。因此,请确保你将获得的任何结果立即写入磁盘或数据库中。有些缓存时间为1天...


那么我该怎么做呢?我应该使用下面帖子中描述的方法(WebClient类及其DownloadFile方法),还是有其他方法可以在将数据插入数据库之前存储它们? http://stackoverflow.com/questions/1576534/how-to-read-xml-data-from-a-url-by-using-vb-net-and-save/1576729#1576729 - Jerry

1

@Jarek 你可以将XML文件存储为本地缓存,放在Web服务器的文件结构中,例如.../Users/thisusersid/mycustomfile_02102011.xml。当用户再次登录时,只需获取该文件,拆分文件名,检查日期并使用最新的文件作为默认文件。然后,您可以允许用户通过单击按钮/链接手动从eveapi更新其数据以获取最新和最好的数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接