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 create XBRL instance document and add objects in C#.
The following topics shall be covered in this article:
- C# API to Create XBRL Instance Document and Add Objects
- Create XBRL Instance Document
- Add Schema Reference to XBRL
- Add Context Object in XBRL
- Create a Unit in XBRL
- Add Fact Object in XBRL
C# API to Create XBRL Instance Document and Add Objects
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
Create XBRL Instance Document in C#
We can easily create an XBRL instance document from scratch by following the steps given below:
- Firstly, create an instance of the XbrlDocument class.
- Next, access instances’ collection from the XbrlDocument object into the XbrlInstanceCollection object.
- After that, add a new XBRL instance using the XbrlInstanceCollection.Add() method.
- Finally, save the XBRL file using the XbrlDocument.Save() method. It takes the output file path as an argument.
The following code sample shows how to create an XBRL instance document using C#.
Add Schema Reference to XBRL using C#
We can add schema reference in an XBRL instance document by following the steps given below:
- Firstly, create an instance of the XbrlDocument class.
- Next, get the XbrlDocument object’s instances’ collection into XbrlInstanceCollection object.
- Then, add a new XBRL instance using XbrlInstanceCollection.Add() method.
- Next, get the XbrlInstance object’s schema reference collection into SchemaRefCollection object.
- After that, add a new schema reference using SchemaRefCollection.Add() method.
- 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 CSharp
We can add a context to an XBRL instance document by following the steps given below:
- Firstly, create an instance of the XbrlDocument class.
- Next, get XbrlDocument object’s instances’ collection into XbrlInstanceCollection object.
- Then, add a new XBRL instance using XbrlInstanceCollection.Add() method.
- Next, create an instance of the ContextPeriod class with the start and end dates.
- Then, create a ContextEntity and provide schema and identifier.
- Next, create an instance of the Context class with the defined ContextPeriod and ContextEntity.
- After that, add the Context object to the Context objects collection.
- 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:
- Firstly, create an instance of the XbrlDocument class.
- Next, get XbrlDocument object’s instances’ collection into XbrlInstanceCollection object.
- Then, add a new XBRL instance using XbrlInstanceCollection.Add() method.
- Next, create an instance of the Unit class with the UnitType as Measure.
- Then, add QualifiedName to the MeasureQualifiedNames collection.
- After that, add the Unit to the Unit objects collection.
- 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:
- Firstly, create an instance of the XbrlDocument class.
- Add a new XBRL instance to the XbrlDocument object’s instances.
- Add a new schema reference to the XbrlInstance object’s schema references.
- Get SchemaRef by its index from SchemaRefCollection.
- Initialize the Context instance and add it to the Context objects collection.
- Define a Unit instance and add it to the Unit objects collection.
- Create a Concept class instance using the SchemaRef.GetConceptByName() method.
- Create an instance of the Item class with the Concept object as an argument.
- Set item properties such as ContextRef, UnitRef, Precision, Value, etc.
- After that, add the Item to the Fact objects collection.
- 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 documentation. In case of any ambiguity, please feel free to contact us on our free support forum.