Add Objects to XBRL Documents using C#

Add Objects to XBRL Documents using C#

XBRL (eXtensible Business Reporting Language) defines and exchanges financial information, such as a financial statement. An XBRL instance document is a collection of facts that together make up a business report. We can easily create an XBRL instance document programmatically and add objects or elements such as schema reference, context, unit, items, etc. In this article, we will learn how to add objects to XBRL instance documents using C#.

The following topics shall be covered in this article:

C# API to Add Objects to XBRL Instance Documents

For creating an XBRL instance document and adding objects, we will be using the Aspose.Finance for .NET API. It allows creating XBRL instances, parsing, and validating the XBRL or iXBRL files. Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Finance

Add Schema Reference to XBRL using C#

We can add schema reference in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Next, get the XbrlDocument object’s instances’ collection into XbrlInstanceCollection object.
  3. Then, add a new XBRL instance using XbrlInstanceCollection.Add() method.
  4. Next, get the XbrlInstance object’s schema reference collection into SchemaRefCollection object.
  5. After that, add a new schema reference using SchemaRefCollection.Add() method.
  6. Finally, save the XBRL file using XbrlDocument.Save() method. It takes the output file path as an argument.

The following code sample shows how to add schema reference in an XBRL instance document using C#.

Add Context Object in XBRL using C#

We can add a context to an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Next, get XbrlDocument object’s instances’ collection into XbrlInstanceCollection object.
  3. Then, add a new XBRL instance using XbrlInstanceCollection.Add() method.
  4. Next, create an instance of the ContextPeriod class with the start and end dates.
  5. Then, create a ContextEntity and provide schema and identifier.
  6. Next, create an instance of the Context class with the defined ContextPeriod and ContextEntity.
  7. After that, add the Context object to the Context objects collection.
  8. Finally, save the XBRL file using XbrlDocument.Save() method. It takes the output file path as an argument.

The following code sample shows how to add a context object in an XBRL instance document using C#.

Create Units in XBRL using C#

The units in XBRL measure numeric Items. We can add a unit in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Next, get XbrlDocument object’s instances’ collection into XbrlInstanceCollection object.
  3. Then, add a new XBRL instance using XbrlInstanceCollection.Add() method.
  4. Next, create an instance of the Unit class with the UnitType as Measure.
  5. Then, add QualifiedName to the MeasureQualifiedNames collection.
  6. After that, add the Unit to the Unit objects collection.
  7. Finally, save the XBRL file using XbrlDocument.Save() method. It takes the output file path as an argument.

The following code sample shows how to add a unit object in an XBRL instance document using C#.

Add Fact Object in XBRL using C#

Facts in XBRL are defined using item elements. An item in XBRL contains the value of the simple fact and a reference to the context to correctly interpret that fact. We can add an item in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Add a new XBRL instance to the XbrlDocument object’s instances.
  3. Add a new schema reference to the XbrlInstance object’s schema references.
  4. Get SchemaRef by its index from SchemaRefCollection.
  5. Initialize the Context instance and add it to the Context objects collection.
  6. Define a Unit instance and add it to the Unit objects collection.
  7. Create a Concept class instance using the SchemaRef.GetConceptByName() method.
  8. Create an instance of the Item class with the Concept object as an argument.
  9. Set item properties such as ContextRef, UnitRef, Precision, Value, etc.
  10. After that, add the Item to the Fact objects collection.
  11. Finally, save the XBRL file using XbrlDocument.Save() method. It takes the output file path as an argument.

The following code sample shows how to add a fact as an item element in an XBRL instance document using C#.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to create an XBRL document using C#. We have also seen how to add various XBRL objects to the created XBRL instance document programmatically. Besides, you can learn more about Aspose.Finance for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also