2017-01-24 54 views
1

我試圖解析以下頁面:如何在c#中解析sdmx文件?

http://sdw-wsrest.ecb.europa.eu/service/data/YC/B.U2.EUR.4F.G_N_A.SV_C_YM.BETA0+BETA1+BETA2+BETA3+TAU1+TAU2?lastNObservations=1

,以獲得BETA0值,BETA1,等....

我很努力,因爲它出現在網頁中SDMX格式,而不是常規的XML。 如果任何人都可以幫助一個C#代碼片段,將不勝感激。

預先感謝您。

+0

SDMX-ML根據這個鏈接是XML:https://sdmx.org/?page_id=5008 –

+0

@JohnKoerner,我曾嘗試使用塊sdmxsource來解析sdmx文件,但沒有成功。 我也嘗試使用默認的.Net xml庫,但在這裏再次沒有成功。 – akasolace

回答

1

您可以使用可用於Java和.NET的SDMXSource庫。否則,如果使用更簡單的結構特定格式而不是通用格式檢索相同的數據,則可能更容易解析數據,類似於CSV格式。爲了得到結構特定的格式,將Accept頭部設置爲'application/vnd.sdmx.structurespecificdata + xml; version = 2.1'。

您可能會感興趣此產品cheat sheet

UPDATE:

這裏是解析所述晶格結構特定的格式,以產生一個密鑰/值記錄,而無需使用庫的一個例子。它並未涵蓋所有可能的情況(例如數據集級別屬性),但它是一個很好的開始,適用於此特定數據消息。

XML:

<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> 

代碼:

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; 
      } 
     } 
    } 
} 
+0

謝謝杜拉伊德,我會嘗試改變Accept頭。 關於SDMXSource: 1.文檔進行展示,retriving數據 2.看來真的爲我的小目的 作爲替代一個太大庫實際應用例子,我也在考慮使用正則表達式。 謝謝.. – akasolace

+0

不客氣。如果不使用庫,我會使用XmlReader遍歷Series和Obs節點,然後讀取這些xml節點的屬性並檢查屬性名稱和值。 – duraid

+0

它工作得很好,非常感謝您的幫助! – akasolace