c# – 使用XElement保存XML文件时,文件中的对齐也会发生变化,如
发布时间:2020-12-16 00:07:31 所属栏目:百科 来源:网络整理
导读:我在用 XElement root = XElement.Load(filepath); 加载XML文件,然后找到我需要的东西. IEnumerableXElement commands = from command in MyCommands where (string) command.Attribute("Number") == Number select command;foreach (XElement command in co
我在用
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会对齐所有行,但我需要保存原始文件格式. 除了Group属性值之外,我无法更改文档的任何部分. command.SetAttributeValue("Group") attributes. 解决方法
你需要这样做:
XElement root = XElement.Load(filepath,LoadOptions.PreserveWhitespace); 然后做: root.Save(filepath,SaveOptions.DisableFormatting); 这将通过使用LoadOptions和SaveOptions保留您的原始空白. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |