title: Generating XAdES from USB Token in c#
body: <p>I’m trying to sign XML files via a USB Token in the <strong>XAdES</strong> format.</p>
  <p>Documentation on <strong>System.Security.Cryptography.Xml</strong> is scarce. I can only find how to sign an XML with basic/default options (<a href="https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures</a>).</p>
  <p>It works. However it's missing several elements, e.g.: some References, Transforms and the Object element.</p>
  <p>This is the XAdES XML structure I need.</p>
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  
  &lt;MyRootTag xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  
      &lt;MainElement&gt;
          &lt;!--xml_payload_goes_here--&gt;
      &lt;/MainElement&gt;
      
      &lt;ds:Signature xmlns:xadesv1410=&quot;http://uri.etsi.org/01903/v1.4.1&quot; xmlns:ds=&quot;http://www.w3.org/2000/09/xmldsig#&quot; xmlns:xades=&quot;http://uri.etsi.org/01903/v1.3.2#&quot; Id=&quot;signature-5743-9455-8953-3682&quot;&gt;
      
          &lt;ds:SignedInfo&gt;
              &lt;ds:CanonicalizationMethod Algorithm=&quot;http://www.w3.org/2001/10/xml-exc-c14n#&quot;/&gt;
              &lt;ds:SignatureMethod Algorithm=&quot;http://www.w3.org/2001/04/xmldsig-more#rsa-sha256&quot;/&gt;
              &lt;ds:Reference URI=&quot;&quot;&gt;
                  &lt;ds:Transforms&gt;
                      &lt;ds:Transform Algorithm=&quot;http://www.w3.org/2002/06/xmldsig-filter2&quot;&gt;
                          &lt;dsig-xpath:XPath xmlns:dsig-xpath=&quot;http://www.w3.org/2002/06/xmldsig-filter2&quot; Filter=&quot;subtract&quot;&gt;/descendant::ds:Signature&lt;/dsig-xpath:XPath&gt;
                      &lt;/ds:Transform&gt;
                      &lt;ds:Transform Algorithm=&quot;http://www.w3.org/2001/10/xml-exc-c14n#&quot;/&gt;
                  &lt;/ds:Transforms&gt;
                  &lt;ds:DigestMethod Algorithm=&quot;http://www.w3.org/2001/04/xmlenc#sha256&quot;/&gt;
                  &lt;ds:DigestValue&gt;&lt;!--Base64--&gt;&lt;/ds:DigestValue&gt;
              &lt;/ds:Reference&gt;
              &lt;ds:Reference URI=&quot;#signed-properties-0967-6800-5986-4853&quot; Type=&quot;http://uri.etsi.org/01903#SignedProperties&quot;&gt;
                  &lt;ds:Transforms&gt;
                      &lt;ds:Transform Algorithm=&quot;http://www.w3.org/2001/10/xml-exc-c14n#&quot;/&gt;
                  &lt;/ds:Transforms&gt;
                  &lt;ds:DigestMethod Algorithm=&quot;http://www.w3.org/2001/04/xmlenc#sha256&quot;/&gt;
                  &lt;ds:DigestValue&gt;&lt;!--Base64--&gt;&lt;/ds:DigestValue&gt;
              &lt;/ds:Reference&gt;
          &lt;/ds:SignedInfo&gt;
          
          &lt;ds:SignatureValue Id=&quot;signature-value-3400-3038-9836-4335&quot;&gt;&lt;!--Base64--&gt;&lt;/ds:SignatureValue&gt;
          
          &lt;ds:KeyInfo Id=&quot;key-info-3540-2157-4472-8413&quot;&gt;
              &lt;ds:X509Data&gt;
                  &lt;ds:X509Certificate&gt;&lt;!--Base64--&gt;&lt;/ds:X509Certificate&gt;
              &lt;/ds:X509Data&gt;
              &lt;ds:KeyValue&gt;
                  &lt;ds:RSAKeyValue&gt;
                      &lt;ds:Modulus&gt;&lt;!--Base64--&gt;&lt;/ds:Modulus&gt;
                      &lt;ds:Exponent&gt;AQAB&lt;/ds:Exponent&gt;
                  &lt;/ds:RSAKeyValue&gt;
              &lt;/ds:KeyValue&gt;
          &lt;/ds:KeyInfo&gt;
          
          &lt;ds:Object Id=&quot;signature-object-0000-0000-0000-0000&quot;&gt;
              &lt;xades:QualifyingProperties Target=&quot;#signature-0000-0000-0000-0000&quot;&gt;
                  &lt;xades:SignedProperties Id=&quot;signed-properties-0000-0000-0000-0000&quot;&gt;
                      &lt;xades:SignedSignatureProperties&gt;
                          &lt;xades:SigningTime&gt;2021-11-03T08:25:08Z&lt;/xades:SigningTime&gt;
                          &lt;xades:SigningCertificate&gt;
                              &lt;xades:Cert&gt;
                                  &lt;xades:CertDigest&gt;
                                      &lt;ds:DigestMethod Algorithm=&quot;http://www.w3.org/2001/04/xmlenc#sha256&quot;/&gt;
                                      &lt;ds:DigestValue&gt;&lt;!--Base64--&gt;&lt;/ds:DigestValue&gt;
                                  &lt;/xades:CertDigest&gt;
                                  &lt;xades:IssuerSerial&gt;
                                      &lt;ds:X509IssuerName&gt;Issuer_Name&lt;/ds:X509IssuerName&gt;
                                      &lt;ds:X509SerialNumber&gt;000000000000000000000000000000000000000&lt;/ds:X509SerialNumber&gt;
                                  &lt;/xades:IssuerSerial&gt;
                              &lt;/xades:Cert&gt;
                          &lt;/xades:SigningCertificate&gt;
                      &lt;/xades:SignedSignatureProperties&gt;
                  &lt;/xades:SignedProperties&gt;
              &lt;/xades:QualifyingProperties&gt;
          &lt;/ds:Object&gt;
          
      &lt;/ds:Signature&gt;
      
  &lt;/MyRootTag&gt;
  </code></pre>
  <p>I need to figure out how to add these elements to SignedXml using AddReference (+AddTransform), AddObject, etc.</p>
  <p>Where can I find some c# code examples for XAdES signing, particularly regarding different SignedXml formatting options?</p>