Search code examples
c#.netpostmanwebapixbrl

Created API for input XBRL file and need to get list of contexts, but got empty array


I create Web Api for input XBRL file and need to get output list of contexts according to the period that I will put in postman. But I got empty array. I can not understand where is the problem.

Any suggestion will be helpful.

public class Envelope
{
    public string Period { get; set; }
    public byte[] XBRLInstance { get; set; }
}

[ApiController]
[Route("api/xbrl")]
public class XBRLController : ControllerBase
{
    [HttpPost]
    [Consumes("application/json")]
    public IActionResult Post([FromBody] Envelope envelope)
    {
        // extract period and XBRLInstance from the envelope
        string period = envelope.Period;
        string xbrlInstance = Encoding.UTF8.GetString(envelope.XBRLInstance);

        // parse XBRLInstance into XDocument
        XDocument xbrlData = XDocument.Parse(xbrlInstance);

        // logic to filter XBRL data by period and return the relevant contexts
        List<string> filteredContexts = GetContextsByPeriod(xbrlData, period);

        // Return the list of contexts in the response
        return Ok(filteredContexts);
    }

    [HttpGet]
    private List<string> GetContextsByPeriod(XDocument xbrlData, string period)
    {
        XNamespace xbrli = "http://www.xbrl.org/2003/linkbase";
        //  logic to filter XBRL data by period and return the relevant contexts
        var filteredContexts = xbrlData.Descendants(xbrli + "context")
                                       .Where(context =>
                                       {
                                           // condition to filter by period
                                           string contextPeriod = context.Element(xbrli + "period").Value;
                                           return contextPeriod == period;
                                       })
                                       .Select(context => context.Attribute("id").Value)
                                       .ToList();

        return filteredContexts;
    }
}```

And in postman it looks like this
```{
 "Period": "2022-01-01/2022-12-31",
    "XBRLInstance": "PD94bWwgdm...."
}```
And I got empty array in body []

Solution

  • should be:

    XNamespace xbrli = "http://www.xbrl.org/2003/instance";
    

    You are using the wrong namespace "http://www.xbrl.org/2003/linkbase" as far as I can tell.