2012-03-13 87 views
0

我想对大量XAML文件做一些批量更改,并通过使用linq的XDocument进行连接来完成此操作。此工作正常,我可以添加新元素,但是我遇到问题压痕:与XDocument缩进

文件装载 “PreserveWhitseSpace” 因为我需要保持的格式(因为SVN的):

_xamlDocument = XDocument.Load(新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 

我已经尝试设置disableformating为保存选项,但似乎没有导致期望的结果。

任何想法? 最好的问候

+0

我发现这个额外的信息,但我正在寻找一种方法来保留空白,并很好地格式化新元素http://bytes.com/topic/net/answers/178641-xmltextwriter-formatting-preservewhitespace – ccg 2012-03-13 15:49:29

回答

0

有一个XDocument.Save()超载需要XmlWriter参数,而不是TextWriterXmlWriter.Settings属性允许您控制缩进等内容。您应该使用XmlWriter来包装您的基础流(FileStream,MemoryStream等)。

+0

好吧我会试试看,谢谢你的提示。 – ccg 2012-03-13 15:52:18

+0

我试过它没有成功,请你看看这里:http://bytes.com/topic/net/answers/178641-xmltextwriter-formatting-preservewhitespace如果你仍然认为它可能使用XMLSettings这样做,什么我需要指定吗?我指定xmlSettings.Indent = true; – ccg 2012-03-13 16:02:06