0

我有一个关于轮询或检测网站上 XML 文件更改的问题。我在下面有一个非常硬编码的系统,它从纽约联储读取关于他们日常证券借贷的 XML 文件。它运行良好,我对此没有任何问题。我的主要问题是我如何在每个工作日轮询网站的变化。我知道我可以检查 XML 文件上的“Last Modified”字段(或者在例中为“Prepared”字段),但我很好奇我是如何做到这一点的,以便在实际进行更改时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();
        }
    }
}
4

1 回答 1

2

除非美联储提供某种事件表明有新文件可用(我怀疑是这种情况),否则理论上最好的做法是下载最新的 XML 文件并检查它是否比以前的文件更新。

如果该文件确实每天发布一次,您可以生成自己的统计信息,了解该文件每天发布的确切时间。在围绕该典型发布时间的窗口中加强您的轮询(但请确保您在轮询频率中没有违反任何服务条款)。

于 2012-07-16T17:53:49.913 回答