【问题标题】:Indentation with XDocument使用 XDocument 缩进
【发布时间】:2012-03-13 15:37:10
【问题描述】:

我正在尝试对许多 XAML 文件进行一些批量更改,并且我尝试通过使用 linq 的 XDocument 对其进行编辑来做到这一点。这工作正常,我可以添加新元素,但是我遇到了缩进问题:

文档加载了“PreserveWhitseSpace”,因为我需要保持格式(因为 SVN):

_xamlDocument = XDocument.Load(new StringReader(newContent), LoadOptions.PreserveWhitespace);

然后我添加一些元素,完成后我使用以下命令保存文件:

_xamlDocument.Save(stringWriter, SaveOptions.None);

新添加的元素没有很好地缩进,看起来不太好(抱歉这里的格式,但你会看到 /n 字符串):

<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<UserControl x:Class=\"Sonova.Chinook.UserInterface.Common.SimpleProgressView\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Content=\"{Binding}\">\r\n    <UserControl.Resources>\r\n        \r\n    <ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source=\"{ThemeDictionary AssemblyName=Cocoon.UserInterface}\" /><ResourceDictionary Source=\"/Cocoon.UserInterface;component/VisualResources/Controls/ContentControl.xaml\" /></ResourceDictionary.MergedDictionaries></ResourceDictionary></UserControl.Resources>\r\n    <UserControl.Style>\r\n        <StaticResource ResourceKey=\"SimpleProgressStyle\" />\r\n    </UserControl.Style>\r\n</UserControl>

上面的 Xml 显示没有换行符或空格,例如以下元素:

<ResourceDictionary Source=\"{ThemeDictionary AssemblyName=Cocoon.UserInterface}\" /><ResourceDictionary

我已经尝试将禁用格式设置为保存选项,但似乎没有一个会导致所需的结果。

有什么想法吗? 最好的问候

【问题讨论】:

标签: .net linq xaml indentation linq-to-xml


【解决方案1】:

XDocument.Save() 的重载采用 XmlWriter 参数而不是 TextWriterXmlWriter.Settings 属性允许您控制缩进等内容。您应该使用XmlWriter 来包装您的底层流(FileStreamMemoryStream 等)。

【讨论】:

  • 我尝试过但没有成功,请您看一下:bytes.com/topic/net/answers/… 如果您仍然认为可以使用 XMLSettings 这样做,我需要指定什么?我指定了 xmlSettings.Indent = true;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多