I have one file and I used this code to replace />
with />
in line 6 but it add empty line at the end...Why? and how to solve it?
the file before edit the whitespace:
<xades:SignedProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Id="xadesSignedProperties">
<xades:SignedSignatureProperties>
<xades:SigningTime>2022-05-31T20:38:47Z</xades:SigningTime>
<xades:SigningCertificate>
<xades:Cert>
<xades:CertDigest>
<ds:DigestMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
<ds:DigestValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#">NjlhOTVmYzIzN2I0MjcxNGRjNDQ1N2EzM2I5NGNjNDUyZmQ5ZjExMDUwNGM2ODNjNDAxMTQ0ZDk1NDQ4OTRmYg==</ds:DigestValue>
</xades:CertDigest>
<xades:IssuerSerial>
<ds:X509IssuerName xmlns:ds="http://www.w3.org/2000/09/xmldsig#">CN=TSZEINVOICE-SubCA-1, DC=extgazt, DC=gov, DC=local</ds:X509IssuerName>
<ds:X509SerialNumber xmlns:ds="http://www.w3.org/2000/09/xmldsig#">2475382876776561391517206651645660279462721580</ds:X509SerialNumber>
</xades:IssuerSerial>
</xades:Cert>
</xades:SigningCertificate>
</xades:SignedSignatureProperties>
</xades:SignedProperties>
The XML file after remove the whitespace:
<xades:SignedProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Id="xadesSignedProperties">
<xades:SignedSignatureProperties>
<xades:SigningTime>2022-05-31T20:38:47Z</xades:SigningTime>
<xades:SigningCertificate>
<xades:Cert>
<xades:CertDigest>
<ds:DigestMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#">NjlhOTVmYzIzN2I0MjcxNGRjNDQ1N2EzM2I5NGNjNDUyZmQ5ZjExMDUwNGM2ODNjNDAxMTQ0ZDk1NDQ4OTRmYg==</ds:DigestValue>
</xades:CertDigest>
<xades:IssuerSerial>
<ds:X509IssuerName xmlns:ds="http://www.w3.org/2000/09/xmldsig#">CN=TSZEINVOICE-SubCA-1, DC=extgazt, DC=gov, DC=local</ds:X509IssuerName>
<ds:X509SerialNumber xmlns:ds="http://www.w3.org/2000/09/xmldsig#">2475382876776561391517206651645660279462721580</ds:X509SerialNumber>
</xades:IssuerSerial>
</xades:Cert>
</xades:SigningCertificate>
</xades:SignedSignatureProperties>
</xades:SignedProperties>
my code is:
var lines = File.ReadAllLines(@"D:\test.xml");
for (int i = 0; i < lines.Length; i )
{
> Blockquote
if (lines[i].Contains(" />")) lines[i] = lines[i].Replace(" />", "/>");
}
File.WriteAllLines(@"D:\test.xml", lines);
CodePudding user response:
Because of File.WriteAllLines. It writes a collection of strings as lines, with each line terminated by an "end of line" character sequence, including the last string/line.
Instead of File.ReadAllLines/File.WriteAllLines, you could simply use File.ReadAllText/File.WriteAllText in your situation. As an additional bonus, unlike File.ReadAllLines/File.WriteAllLines, using the File.ReadAllText/File.WriteAllText combo will preserve the existing "end of line" character sequences regardless of them being Unix/Linux- or Windows-style line breaks no matter on which operating system your program runs.