问题描述
限时送ChatGPT账号..我正在使用
XElement root = XElement.Load(filepath);
加载 XML 文件,然后找到我需要的东西.
to load XML file, then finding things that I need.
IEnumerable<XElement> commands = from command in MyCommands
where (string) command.Attribute("Number") == Number
select command;
foreach (XElement command in commands)
{
command.SetAttributeValue("Group", GroupFound);
}
完成更改后,我使用以下代码保存文件.
When I am done with my changes, I save the file with the following code.
root.Save(filepath);
保存文件时,我的 XML 文件中的所有行都会受到影响.Visual Studio 默认对齐所有行,但我需要保存原始文件格式.
When file is saved, all the lines in my XML file are affected. Visual Studio aligns all the lines by default, but I need to save the original file format.
我不能改变文档的任何部分,除了 Group 属性值.
I cannot alter any part of the document, except the Group attribute values.
command.SetAttributeValue("Group") attributes.
推荐答案
您需要:
XElement root = XElement.Load(filepath, LoadOptions.PreserveWhitespace);
然后做:
root.Save(filepath, SaveOptions.DisableFormatting);
这将通过使用 保留您的原始空白LoadOptions 和 SaveOptions.
This will preserve your original whitespace through the use of LoadOptions and SaveOptions.
这篇关于使用 XElement 保存 XML 文件时,文件中的对齐方式也会发生变化,如何避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论