---
title: "XML signatures"
description: "XmlDSig and XAdES signatures in PKI SDK over a whole XML document, a specific element or external content, with basic examples."
canonical: "https://docs.lacunasoftware.com/en-us/articles/pki-sdk/signatures/xml"
locale: "en"
product: "PKI SDK"
last_updated: "2026-10-12"
---

# XML signatures

PKI SDK supports the XmlDSig and XAdES signature standards. An XML signature can cover a whole document, a specific element inside
the document, or external content. As basic examples, the sections below show the signer signing a whole XML document and a
specific element of an XML document.

## Signing an XML document

The code below shows a basic XAdES signature of a whole XML document.

```cs
// ICP-Brasil AD-RB XAdES signature policy
var policy = BrazilXmlPolicySpec.GetAdrBasicaV22(true);

// Instantiate the signer object, which creates the signature
var signer = new FullXmlSigner();

signer.SetSigningCertificate(cert);    // the signer's certificate, with its private key
signer.SetXml(xmlDocument);            // bytes or XmlDocument object of the XML document to sign
signer.SetPolicy(policy);              // AD-RB signature policy
signer.ComputeSignature();             // creates the signature element
var signedXml = signer.GetSignedXml(); // returns the bytes of the signed XML document
```

In this signature mode, by default, the transformation of the signed element is enveloped, and the resulting signature element is
included as a child of the root element in the finished document. For more information on where the signature element goes and on
the XML signature modes, see [Insertion modes](https://docs.lacunasoftware.com/en-us/articles/pki-sdk/signatures/xml/insertion).

## Signing an element of an XML document

The code below shows a basic XAdES signature of a specific element of an XML document.

> **Note**
>
> To sign an element of an XML document, the element must have an ID attribute that is unique in the XML.

```cs
// ICP-Brasil AD-RB XAdES signature policy
var policy = BrazilXmlPolicySpec.GetAdrBasicaV22(true);

// Instantiate the signer object, which creates the signature
var signer = new XmlElementSigner();

signer.SetSigningCertificate(cert);    // the signer's certificate, with its private key
signer.SetXml(xmlDocument);            // bytes or XmlDocument object of the XML document that has the element to sign
signer.SetToSignElementId(id);         // sets the ID of the element to sign
signer.SetPolicy(policy);
signer.ComputeSignature();             // creates the signature element
var signedXml = signer.GetSignedXml(); // returns the bytes of the XML document with the signature element
```

In this signature mode, by default, a *detached* signature is created, and the signature element is included as a sibling of the
signed element. For more information on where the signature element goes and on the XML signature modes, see
[Insertion modes](https://docs.lacunasoftware.com/en-us/articles/pki-sdk/signatures/xml/insertion).

## Signing a Brazilian electronic invoice (NFe)

The code below signs a Brazilian electronic invoice (*Nota Fiscal eletrônica*, NFe) in the national standard:

```cs
// signature policy for NFe
var policy = BrazilXmlPolicySpec.GetNFePadraoNacional();

// Instantiate the signer object, which creates the signature
var signer = new XmlElementSigner();

signer.SetSigningCertificate(cert);      // the signer's certificate, with its private key
signer.SetXml(xmlNFe);                   // bytes or XmlDocument object of the NFe to sign
signer.SetToSignElementId("NFe0000001"); // sets the ID of the NFe to sign
signer.SetPolicy(policy);
signer.ComputeSignature();               // creates the signature element
var signedNFe = signer.GetSignedXml();   // returns the bytes of the signed NFe
```

## See also

* @Lacuna.Pki.Xml.XmlElementSigner
* @Lacuna.Pki.Xml.FullXmlSigner

**Demos and samples**

- Live demo: [Validate a signed file](https://try.lacunasoftware.com/en/pki-suite/validate-a-signed-file)
- Validate a signed PDF using PKI SDK: [.NET](https://github.com/LacunaSoftware/PkiSuiteSamples/blob/master/dotnet/mvc/PkiSuiteAspNetMvcSample/Controllers/OpenPadesSignatureSdkController.cs)
- Validate a CMS/CAdES signature using PKI SDK: [.NET](https://github.com/LacunaSoftware/PkiSuiteSamples/blob/master/dotnet/mvc/PkiSuiteAspNetMvcSample/Controllers/OpenCadesSignatureSdkController.cs)
- Validate a signed XML using PKI SDK: [.NET](https://github.com/LacunaSoftware/PkiSuiteSamples/blob/master/dotnet/mvc/PkiSuiteAspNetMvcSample/Controllers/OpenXmlSignatureSdkController.cs)
- Sign an XML document using PKI SDK: [.NET](https://github.com/LacunaSoftware/PkiSuiteSamples/blob/master/dotnet/mvc/PkiSuiteAspNetMvcSample/Controllers/XmlSignatureSdkController.cs)
- Sign an XML document with a server key using PKI SDK: [.NET](https://github.com/LacunaSoftware/PkiSuiteSamples/blob/master/dotnet/mvc/PkiSuiteAspNetMvcSample/Controllers/XmlServerKeySdkController.cs)
- Sign a Brazilian NF-e using PKI SDK: [.NET](https://github.com/LacunaSoftware/PkiSuiteSamples/blob/master/dotnet/mvc/PkiSuiteAspNetMvcSample/Controllers/XmlNFeSignatureSdkController.cs)
- Sign a COD XML using PKI SDK: [.NET](https://github.com/LacunaSoftware/PkiSuiteSamples/blob/master/dotnet/mvc/PkiSuiteAspNetMvcSample/Controllers/XmlCodSignatureSdkController.cs)
