Preencher campo Signature do XML

Boa Tarde,

Estou desenvolvendo sistema para transmissão de lote de RPS de NFSe, consumindo webService da prefeitura de UBERLANDIA.

Toda estrutura xml já está pronta, só estou com dúvidas no preenchimento do campo Signature, mais especificamente as tags DigestValue, SignatureValue e X509Certificate. Estou desenvolvendo em C#.
Como devo preencher esses campos ?

Obrigado !

Você pode usar o código abaixo.

A variável cert é do tipo:

X509Certificate2 cert;

Você carrega o XML pela variável filename.

        //Assinar
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.PreserveWhitespace = true;
        xmlDoc.Load(filename);

        XmlNodeList tags = xmlDoc.GetElementsByTagName("EnviarLoteRpsEnvio");
        XmlElement tag = (XmlElement)tags[0]; //Primeiro da lista
        //RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)cert.PrivateKey;
        SignedXml signedXml = new SignedXml(tag);
        signedXml.SigningKey = cert.PrivateKey;

        // Transformações p/ DigestValue da Nota
        Reference reference = new Reference("");
        reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
        reference.AddTransform(new XmlDsigC14NTransform());
        signedXml.AddReference(reference);

        KeyInfo keyInfo = new KeyInfo();
        keyInfo.AddClause(new KeyInfoX509Data(cert));
        signedXml.KeyInfo = keyInfo;
        signedXml.ComputeSignature();

        XmlElement xmlSignature = xmlDoc.CreateElement("Signature", "http://www.w3.org/2000/09/xmldsig#");
        XmlElement xmlSignedInfo = signedXml.SignedInfo.GetXml();
        XmlElement xmlKeyInfo = signedXml.KeyInfo.GetXml();

        XmlElement xmlSignatureValue = xmlDoc.CreateElement("SignatureValue", xmlSignature.NamespaceURI);
        string signBase64 = Convert.ToBase64String(signedXml.Signature.SignatureValue);
        XmlText text = xmlDoc.CreateTextNode(signBase64);
        xmlSignatureValue.AppendChild(text);

        xmlSignature.AppendChild(xmlDoc.ImportNode(xmlSignedInfo, true));
        xmlSignature.AppendChild(xmlSignatureValue);
        xmlSignature.AppendChild(xmlDoc.ImportNode(xmlKeyInfo, true));
        tag.AppendChild(xmlSignature);

        var xml = xmlDoc.OuterXml;

Muito Obrigado Luiz, o método funcionou, assinou o xml da forma que eu esperava. Estou com o problema de o webService estar retornando o erro " 1405
Assinatura Digital InvalidaAssinatura Invalida. ". O xml está conforme o exemplo do manual da prefeitura de Uberlândia, a tag ASSIGNATURE inclusive. Estou enviando o anexo de exemplo do xml que estou gerando, caso consiga ver algum erro na estrutura que eu não estou conseguindo identificar. Quando mando o xml para o método de teste do webService, o qual não precisa de assinatura com certificado digital, ele valida perfeitamente.

Desde já, muito obrigado pela atenção!testeXML.xml (7,2 KB)

Te mandei uma resposta por mensagem privada.

Estou com o mesmo erro “” 1405
Assinatura Digital InvalidaAssinatura Invalida. ", poderia me informar como acabar com esse problema?

Poste aqui o XML de envio para poder analisar.