我正在使用
XElement root = XElement.Load(filepath);
加载 XML 文件,然后找到我需要的东西。
IEnumerable<XElement> commands = from command in MyCommands
where (string) command.Attribute("Number") == Number
select command;
foreach (XElement command in commands)
{
command.SetAttributeValue("Group", GroupFound);
}
完成更改后,我使用以下代码保存文件。
root.Save(filepath);
保存文件时,我的 XML 文件中的所有行都会受到影响。 Visual Studio 默认对齐所有行,但我需要保存原始文件格式。
我无法更改文档的任何部分,除了组属性值。
command.SetAttributeValue("Group") attributes.
最佳答案
你需要做的:
XElement root = XElement.Load(filepath, LoadOptions.PreserveWhitespace);
然后做:
root.Save(filepath, SaveOptions.DisableFormatting);
这将通过使用 LoadOptions 保留您的原始空白和 SaveOptions .
关于c# - 使用 XElement 保存 XML 文件时,文件中的对齐方式也会发生变化,如何避免?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16155516/