Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用UTF8编码c中没有BOM前导的签名xml文件#_C#_Xml_Encoding - Fatal编程技术网

C# 如何用UTF8编码c中没有BOM前导的签名xml文件#

C# 如何用UTF8编码c中没有BOM前导的签名xml文件#,c#,xml,encoding,C#,Xml,Encoding,我有一个为xml文件签名的函数,该文件采用utf-8编码,但当我应用签名时,它会显示“utf-8 with Boom”(错误版本),如何正确保存该文件 private static void FirmarDocumentoComplemento(string pathXmlDocument, string pathCert, string passCert, string pathXmlSignet, string ruta, string rut, string fecha, string t

我有一个为xml文件签名的函数,该文件采用utf-8编码,但当我应用签名时,它会显示“utf-8 with Boom”(错误版本),如何正确保存该文件

private static void FirmarDocumentoComplemento(string pathXmlDocument, string pathCert, string passCert, string pathXmlSignet, string ruta, string rut, string fecha, string tipo)
{

    XmlDocument documentXml = new XmlDocument();
    documentXml.Load(pathXmlDocument);

    SignedXml firmado = new SignedXml(documentXml);

    var cert = RSA_helper.GetX509Certificate(pathCert, passCert);
    firmado.SigningKey = (RSA)cert.PrivateKey;
    firmado.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";

    //digest info
    Reference reference = new Reference();
    reference.Uri = "";
    reference.DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1";

    firmado.AddReference(reference);

    // with the public key will be added in the signature part. 
    KeyInfo keyInfo = new KeyInfo();
    keyInfo.AddClause(new RSAKeyValue((RSA)cert.PrivateKey));
    keyInfo.AddClause(new KeyInfoX509Data(cert));

    firmado.KeyInfo = keyInfo;
    firmado.ComputeSignature();

    XmlElement xmlDigitalSignature = firmado.GetXml();

    // buscamos el ultimo elemento del documento para agregarle la firma
    XmlElement elemento = (XmlElement)documentXml.SelectSingleNode(@"//datos_complementarios_declaracion_ingreso/representantes[last()]");

    XmlNode parent = elemento.ParentNode;
    parent.InsertAfter(xmlDigitalSignature, elemento);


    string ruta_completa = ruta + rut + " - " + tipo + " - " + fecha + " - N - " + "Complemento Firmado.xml";
    documentXml.Save(ruta_completa);

}

您应该使用的实例在其构造函数中传递一个实例来编写文档,在该实例中,您显式地将Encoding属性设置为一个没有BOM表前导的实例

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = new UTF8Encoding(false);
string ruta_completa = $"{ruta}{rut} - {tipo} - {fecha} - N - Complemento Firmado.xml";

XmlWriter writer = XmlWriter.Create(ruta_completa, settings);
doc.Save(writer);

您是否使用了以下内容:它似乎使用的是tgz(zip)文件,可能是由于错误的zip版本导致的错误。非常感谢