首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在c#中解析sdmx文件?

如何在c#中解析sdmx文件?
EN

Stack Overflow用户
提问于 2017-01-24 23:28:04
回答 1查看 1.2K关注 0票数 1

我正试图解析以下页面:

YM.BETA0+BETA1+BETA2+BETA3+TAU1+TAU2?lastNObservations=1

为了得到BETA0BETA1等的值.

我正在挣扎,因为它的网页似乎是SDMX格式,而不是常规的XML。如果有人能帮上一个c#片段,我们会非常感激的。

提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-25 21:42:23

您可以使用SDMXSource库,这是可用于Java和.NET的。

否则,如果使用更简单的结构特定格式(而不是泛型格式)检索相同的数据,则可能更容易解析类似于CSV格式的数据。为了获得特定于结构的格式,将Accept标头设置为'application/vnd.sdmx.structurespecificdata+xml;version=2.1'.

您可能会发现这个备忘单很有用。

更新:

下面是一个解析特定于结构的格式以生成键/值记录而不使用库的示例。它没有涵盖所有可能的情况(例如,dataset级别的属性),但它是一个很好的开端,适用于这个特定的数据消息。

XML:

代码语言:javascript
复制
<message:DataSet data:action="Replace" data:validFromDate="2017-01-25T22:31:14.760+01:00" data:structureRef="ECB_FMD2" data:dataScope="DataStructure" xsi:type="ecb_fmd2:DataSetType">
    <Series FREQ="B" REF_AREA="U2" CURRENCY="EUR" PROVIDER_FM="4F" INSTRUMENT_FM="G_N_A" PROVIDER_FM_ID="SV_C_YM" DATA_TYPE_FM="BETA0" COLLECTION="E" TITLE_COMPL="Euro area (changing composition) - Government bond, nominal, all issuers whose rating is triple A - Svensson model - continuous compounding - yield error minimisation - Yield curve parameters, Beta 0 - Euro, provided by ECB" DECIMALS="6" UNIT="PURE_NUMB" TITLE="Yield curve parameters, Beta 0 - Government bond, nominal, all issuers whose rating is triple A - Euro area (changing composition)" UNIT_MULT="0">
        <Obs TIME_PERIOD="2017-01-24" OBS_VALUE="1.775611976078084" OBS_STATUS="A" OBS_CONF="F"/>
    </Series>
    <Series FREQ="B" REF_AREA="U2" CURRENCY="EUR" PROVIDER_FM="4F" INSTRUMENT_FM="G_N_A" PROVIDER_FM_ID="SV_C_YM" DATA_TYPE_FM="BETA1" COLLECTION="E" TITLE_COMPL="Euro area (changing composition) - Government bond, nominal, all issuers whose rating is triple A - Svensson model - continuous compounding - yield error minimisation - Yield curve parameters, Beta 1 - Euro, provided by ECB" DECIMALS="6" UNIT="PURE_NUMB" TITLE="Yield curve parameters, Beta 1 - Government bond, nominal, all issuers whose rating is triple A - Euro area (changing composition)" UNIT_MULT="0">
        <Obs TIME_PERIOD="2017-01-24" OBS_VALUE="-2.438611976090857" OBS_STATUS="A" OBS_CONF="F"/>
    </Series>
    <Series FREQ="B" REF_AREA="U2" CURRENCY="EUR" PROVIDER_FM="4F" INSTRUMENT_FM="G_N_A" PROVIDER_FM_ID="SV_C_YM" DATA_TYPE_FM="BETA2" COLLECTION="E" TITLE_COMPL="Euro area (changing composition) - Government bond, nominal, all issuers whose rating is triple A - Svensson model - continuous compounding - yield error minimisation - Yield curve parameters, Beta 2 - Euro, provided by ECB" DECIMALS="6" UNIT="PURE_NUMB" TITLE="Yield curve parameters, Beta 2 - Government bond, nominal, all issuers whose rating is triple A - Euro area (changing composition)" UNIT_MULT="0">
        <Obs TIME_PERIOD="2017-01-24" OBS_VALUE="11.695146022367336" OBS_STATUS="A" OBS_CONF="F"/>
    </Series>       
</message:DataSet>

代码:

代码语言:javascript
复制
class Program
{
    static void Main(string[] args)
    {
        string path = @"data.xml";

        // An XmlReader created from a file on the disk or any stream like web request for example
        using (var reader = XmlReader.Create(path))
        {
            foreach (var item in GetRecords(reader))
            {
                Debug.WriteLine(string.Join(", ", item.Select(i => string.Format("{0}={1}", i.Key, i.Value))));
            }
        }
    }

    private static IEnumerable<Dictionary<string, string>> GetRecords(XmlReader reader)
    {
        Dictionary<string, string> record = null; 
        while (reader.Read())
        {
            if (reader.IsStartElement() && reader.LocalName == "Series")
            {
                record = new Dictionary<string, string>();
                while (reader.MoveToNextAttribute())
                {
                    record.Add(reader.LocalName, reader.Value);
                }
            }
            else if (reader.IsStartElement() && reader.LocalName == "Obs")
            {                   
                while (reader.MoveToNextAttribute())
                {
                    record.Add(reader.LocalName, reader.Value);
                }

                yield return record;
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41840827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档