我在用
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);
then do:
root.Save(filepath, SaveOptions.DisableFormatting);
这将通过使用保留您的原始空白加载选项 http://msdn.microsoft.com/en-us/library/system.xml.linq.loadoptions.aspx and 保存选项 http://msdn.microsoft.com/en-us/library/system.xml.linq.saveoptions.aspx.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)