c# - 如何在 C# 中从 API 访问和使用 XML

标签 c# .net xml api

我的目标是从 API 中提取 XML 数据并将其加载到 sql server 数据库中。我在这里尝试的第一步是访问数据并显示它。一旦我让它工作,我将遍历每一行并将值插入到 sql server 数据库中。当我尝试运行下面的代码时,没有任何反应,当我将 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>

最佳答案

一些快速搜索(谷歌)表明this is the EVE API cache mechanism kicking in .如果您从相同的 key 和 IP 查询相同的数据,它会告诉您重新使用您已经拥有的数据。因此,请确保您立即将您得到的任何结果写入磁盘或数据库。有的缓存时间是1天...

关于c# - 如何在 C# 中从 API 访问和使用 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2399406/

相关文章:

c# - 从 Gridview 中的某些行隐藏图像按钮

xml - 如何选择具有特定节点的第一个前兄弟节点作为子节点?

c# - HTTPS 远程发布?

C# - 检查 ObservableCollection CollectionChanged 事件是否已经完成执行

.net - C# 中使用 Task 和 Task<TResult> 有什么区别

c# - WPF HTTPWebRequest 底层连接已关闭 : An unexpected error occurred on a receive

android - 在工具栏中添加 searchView 时出现布局问题( fragment )

xml - Android Edittext问题,用done替换keypad enter

c# - 读取 ListBox SelectedItem 并从数据库中选择,其中 id = value

c# - 嵌入式 MongoDB 文档在使用 C# 驱动程序保存时未获得 ID