ins*_*ide 3 c# xml xelement whitespace
我在用
XElement root = XElement.Load(filepath);
Run Code Online (Sandbox Code Playgroud)
加载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);
}
Run Code Online (Sandbox Code Playgroud)
完成更改后,我使用以下代码保存文件.
root.Save(filepath);
Run Code Online (Sandbox Code Playgroud)
保存文件时,我的XML文件中的所有行都会受到影响.默认情况下,Visual Studio会对齐所有行,但我需要保存原始文件格式.
除了Group属性值之外,我无法更改文档的任何部分.
command.SetAttributeValue("Group") attributes.
Run Code Online (Sandbox Code Playgroud)
Rya*_*tes 11
你需要这样做:
XElement root = XElement.Load(filepath, LoadOptions.PreserveWhitespace);
Run Code Online (Sandbox Code Playgroud)
然后做:
root.Save(filepath, SaveOptions.DisableFormatting);
Run Code Online (Sandbox Code Playgroud)
这将通过使用LoadOptions和SaveOptions来保留您的原始空白.