To create an XML document in C# for Fiskalizacija 2.0 (a Croatian fiscalization standard used for generating fiscal receipts), you'll need to construct the XML with the appropriate structure according to the Fiskalizacija 2.0 specifications. The XML document will usually contain information about the transaction, fiscal receipt, issuer, and other related data.

Example XML Structure

The XML structure for Fiskalizacija 2.0 might look like this (this is a simplified example):

HTML / XML
    
        12345678901 
        1 
        2023-11-06T12:34:56 
        202311061234567890 
        Fiskalizacija 
    
    
        
            Product 1
            100.00 
            2 
            25.00 
            200.00 
        
        
            Product 2
            150.00
            1
            25.00
            150.00
        
    
    123456789abcdefg==

C# Code to Generate the XML

Here is how you can generate this XML structure in C# using XmlWriter:

C#
using System;
using System.Xml;

class Fiskalizacija
{
    public static void Main()
    {
        // Initialize the XML writer
        using (XmlWriter writer = XmlWriter.Create("fiskalizacija.xml", new XmlWriterSettings { Indent = true }))
        {
            writer.WriteStartDocument();
            
            // Write the root element 
            writer.WriteStartElement("Fiskalizacija");

            // Write  element
            writer.WriteStartElement("Zaglavlje");

            writer.WriteElementString("OIB", "12345678901"); // OIB of the issuer
            writer.WriteElementString("VrstaDokumenta", "1"); // Type of document (1 = Invoice)
            writer.WriteElementString("DatumVrijeme", "2023-11-06T12:34:56"); // Date and time
            writer.WriteElementString("JIR", "202311061234567890"); // Unique Invoice Reference number
            writer.WriteElementString("Oznaka", "Fiskalizacija"); // Fiscalization status

            writer.WriteEndElement(); // End 

            // Write  element
            writer.WriteStartElement("Stavke");

            // Write first item 
            writer.WriteStartElement("Stavka");
            writer.WriteElementString("Naziv", "Product 1");
            writer.WriteElementString("Cijena", "100.00");
            writer.WriteElementString("Kolicina", "2");
            writer.WriteElementString("Porez", "25.00");
            writer.WriteElementString("Iznos", "200.00");
            writer.WriteEndElement(); // End first 

            // Write second item 
            writer.WriteStartElement("Stavka");
            writer.WriteElementString("Naziv", "Product 2");
            writer.WriteElementString("Cijena", "150.00");
            writer.WriteElementString("Kolicina", "1");
            writer.WriteElementString("Porez", "25.00");
            writer.WriteElementString("Iznos", "150.00");
            writer.WriteEndElement(); // End second 

            writer.WriteEndElement(); // End 

            // Write the  element for the digital signature
            writer.WriteElementString("Potpis", "123456789abcdefg==");

            writer.WriteEndElement(); // End 

            writer.WriteEndDocument();
        }

        Console.WriteLine("XML document created successfully!");
    }
}

Explanation:

  • XmlWriter is used to create and write XML elements.

  • The structure follows the basic Fiskalizacija XML template.

  • For simplicity, I hard-coded some values (like OIB, DatumVrijeme, etc.), but you can replace those with dynamic values.

  • The Potpis element is just a placeholder for the digital signature. In a real scenario, the signature would be generated using an electronic signature mechanism, typically involving private keys and cryptography.

Output (XML file)

When you run the C# program, it will generate an XML file named fiskalizacija.xml with the following content:

HTML / XML
  
    12345678901
    1
    2023-11-06T12:34:56
    202311061234567890
    Fiskalizacija
  
  
    
      Product 1
      100.00
      2
      25.00
      200.00
    
    
      Product 2
      150.00
      1
      25.00
      150.00
    
  
  123456789abcdefg==

Notes:

  1. Digital Signature: The digital signature (Potpis) must be created using cryptographic methods, typically involving private keys and secure certificate handling. This example just uses a placeholder.

  2. Fields and Validation: You may need to validate the XML structure against a schema, especially the unique document identifiers like JIR. You can use libraries like XsdValidation to ensure your XML adheres to the required format.