我有一个对我的 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 关注
- 159 浏览
添加回答
举报
0/150
提交
取消