我有一個對我的 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);}
1 回答

PIPIONE
TA貢獻1829條經驗 獲得超9個贊
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);
您應該使用XmlTextWriter的實例編寫文檔,并在其構造函數中傳遞一個XmlWriterSettings實例,其中您將Encoding屬性顯式設置為不帶 BOM 前導碼的UTF8Encoding實例
- 1 回答
- 0 關注
- 229 瀏覽
添加回答
舉報
0/150
提交
取消