我有一個(gè)對(duì)我的 xml 文件進(jìn)行簽名的函數(shù),該文件采用 utf-8 編碼,但是當(dāng)我應(yīng)用該符號(hào)時(shí),它顯示“utf-8 with Boom”(錯(cuò)誤版本),我如何正確保存文件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);}
1 回答

PIPIONE
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
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);
您應(yīng)該使用XmlTextWriter的實(shí)例編寫文檔,并在其構(gòu)造函數(shù)中傳遞一個(gè)XmlWriterSettings實(shí)例,其中您將Encoding屬性顯式設(shè)置為不帶 BOM 前導(dǎo)碼的UTF8Encoding實(shí)例
- 1 回答
- 0 關(guān)注
- 224 瀏覽
添加回答
舉報(bào)
0/150
提交
取消