当删除一个空间时,为什么在XML的末端添加一条新行
我有一个文件,我使用此代码将/>
替换为第6行中的/>
,但最后添加了空行...为什么?以及如何解决?
在删除Whitespace之后编辑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>
XML文件:
<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>
我的代码是:
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为 file.writealllines 。它将字符串作为线的集合写,每条线以“线的末端”字符序列终止,包括最后一个字符串/行。
在您的情况下,您只需使用file.readalltext/readallText/file.writealltext而不是file.readalllines/file.writealllines而不是file.readalllines/file.writealllines。作为额外的奖励,与file.ReadAlllines/file.writealllines不同,使用file.readallText/file.writealltext组合将保留现有的“ line of Line”字符序列,无论它们是Unix/linix/linix-style-style style style style style style style style lone no break no break no break break break no break您的程序运行哪个操作系统。
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.