我有一个关于轮询或检测网站上XML文件更改的问题。我在下面有一个非常硬编码的系统,它读取纽约联邦储备银行关于其日常证券借贷的XML文件。它运作良好,我对此没有任何问题。我的主要问题是我如何在每个工作日对网站进行调查。我知道我可以检查XML文件中的“上次修改”字段(或this案例中的“准备好”字段),但我很好奇我是如何做到这一点的,以便在更改时实际上是在XML站点上,我的计算机自动加载并解析新的XML文件。一旦出现这些信息对我来说非常重要,因此每隔X分钟进行一次轮询就不会像我想的那样正常工作。有什么想法吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Diagnostics;
namespace ConsoleApplication2 {
class Program {
static void Main(string[] args) {
// XML text reader stuff
//XmlReader textReader = XmlReader.Create("http://www.newyorkfed.org/markets/seclend/xml/v3_0/secLendingXML.cfm");
XmlTextReader textReader = new XmlTextReader("http://www.newyorkfed.org/markets/seclend/xml/v3_0/secLendingXML.cfm");
//SyndicationFeed feed = SindicationFeed.Load(textReader);
//XmlTextReader textReader = new XmlTextReader("LOCATION");
//string[] Fed_Array = new string[] {"Actual Available to Borrow", "Outstanding Loans",
//"Par Submitted", "Par Accepted", "WTD Average Rate"};
int j = 0;
int icount = 0;
using(StreamWriter writer = new StreamWriter("LOCATION"))
//using (StreamWriter writer = new StreamWriter("LOCATION"))
while (textReader.Read()) {
switch (textReader.NodeType) {
case XmlNodeType.Element:
for (int i = 0; i < textReader.AttributeCount; i++) {
textReader.MoveToAttribute(i);
switch (textReader.Name) {
case "securityMaturityDate":
string textvalmatd = textReader.Value;
if (i == 0 && icount == 0) {
writer.Write("N/A,");
Console.Write("N/A,");
icount = 1;
i = -1;
} else {
writer.Write(textvalmatd + ",");
Console.Write(textvalmatd + ",");
icount = 0;
}
break;
case "couponRate":
string textvalcoup = textReader.Value;
writer.Write(textvalcoup + ",");
Console.Write(textvalcoup + ",");
break;
case "securityType":
string textvalsec = textReader.Value;
writer.Write(textvalsec + ",");
Console.Write(textvalsec + ",");
j = 0;
break;
case "value":
string textval = textReader.Value;
writer.Write(textval + ",");
Console.Write(textval + ",");
j = j + 1;
if (j > 4) {
writer.Write(Environment.NewLine);
Console.Write(Environment.NewLine);
}
break;
}
}
break;
}
}
}
private static IDisposable StreamWriter(string p) {
throw new NotImplementedException();
}
}
}
答案 0 :(得分:2)
除非美联储提供某种表明新文件可用的事件(我怀疑是这种情况),你可以做的理论上最好的是下载最新的XML文件并检查它是否比上一个文件更新
如果文件每天真正发布一次,您可以生成自己的统计信息,确切地说每天发布文件的时间。在围绕典型发布时间的窗口中强化您的轮询(但请确保您不违反轮询频率中的任何服务条款)。