如何在 C# 中解析 sdmx 文件?
how to parse sdmx file in c#?
我正在尝试解析以下页面:
为了得到BETA0
、BETA1
等的值....
我很挣扎,因为该网页似乎是 SDMX 格式而不是常规格式 XML。
如果有人可以帮助提供 c# 代码段,我们将不胜感激。
提前致谢。
您可以使用适用于 Java 和 .NET 的 SDMXSource 库。
否则,如果您使用更简单的特定于结构的格式而不是通用格式检索相同的数据,您可能会像 CSV 格式一样更容易地解析数据。为了获得特定于结构的格式,将 Accept header 设置为 'application/vnd.sdmx.structurespecificdata+xml;version=2.1'。
您可能会发现此 cheat sheet 有帮助。
更新:
这里是一个解析结构特定格式以生成 key/value 记录而不使用库的示例。它没有涵盖所有可能的情况(例如数据集级别属性),但它是一个良好的开端并且适用于此特定数据消息。
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;
}
}
}
}
我正在尝试解析以下页面:
为了得到BETA0
、BETA1
等的值....
我很挣扎,因为该网页似乎是 SDMX 格式而不是常规格式 XML。 如果有人可以帮助提供 c# 代码段,我们将不胜感激。
提前致谢。
您可以使用适用于 Java 和 .NET 的 SDMXSource 库。
否则,如果您使用更简单的特定于结构的格式而不是通用格式检索相同的数据,您可能会像 CSV 格式一样更容易地解析数据。为了获得特定于结构的格式,将 Accept header 设置为 'application/vnd.sdmx.structurespecificdata+xml;version=2.1'。
您可能会发现此 cheat sheet 有帮助。
更新:
这里是一个解析结构特定格式以生成 key/value 记录而不使用库的示例。它没有涵盖所有可能的情况(例如数据集级别属性),但它是一个良好的开端并且适用于此特定数据消息。
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;
}
}
}
}