Wpf 保存的FlowDocument的XAML的换行和缩进?

Wpf 保存的FlowDocument的XAML的换行和缩进?,wpf,xaml,flowdocument,Wpf,Xaml,Flowdocument,是否有方法格式化保存FlowDocument时生成的XAML?目前,它都在一行上运行,我想把它分解成元素,换行和缩进,使它更容易阅读 下面是我用来将WPF RichTextBox中的FlowDocument保存为XAML文件的代码: // Create a TextRange around the entire document TextRange textRange = new TextRange(myRichTextBox.Document.ContentStart, myRichTextB

是否有方法格式化保存FlowDocument时生成的XAML?目前,它都在一行上运行,我想把它分解成元素,换行和缩进,使它更容易阅读

下面是我用来将WPF RichTextBox中的FlowDocument保存为XAML文件的代码:

// Create a TextRange around the entire document
TextRange textRange = new TextRange(myRichTextBox.Document.ContentStart, myRichTextBox.Document.ContentEnd);

// Save file
using (FileStream fs = File.Create(fileName))
{
    textRange.Save(fs, DataFormats.Xaml);
}
save可以很好地工作,但正如我所提到的,生成的XAML都一起运行,其各个元素没有缩进或换行:

<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Left" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="Text" NumberSubstitution.Substitution="AsCulture" FontFamily="Calibri" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="12" Foreground="#FF000000" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="Normal" Typography.Capitals="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.EastAsianWidths="Normal" Typography.EastAsianLanguage="Normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0"><Paragraph NumberSubstitution.CultureSource="User" FontFamily="Segoe UI"><Run>Lorem ipsum dolor si ament</Run></Paragraph></Section>
Lorem ipsum dolor si ament

我希望像传统的XAML一样在文件中格式化XAML,为各种元素设置换行和缩进。有没有办法让.NET在生成XAML时添加格式?谢谢。

尝试使用
XmlTextWriter

使用(FileStream fs=File.Create(fileName)
{
XmlTextWriter=新的XmlTextWriter(fs,Encoding.Default);
writer.Formatting=格式化.缩进;
保存(writer,DataFormats.Xaml);
}
第二次尝试: 这似乎产生了一个格式化的XML文档

使用(MemoryStream ms=new MemoryStream())
{
保存(ms,DataFormats.Xaml);
ms.Position=0;
StreamReader sr=新的StreamReader(毫秒);
XmlDocument doc=新的XmlDocument();
XmlTextWriter=新的XmlTextWriter(文件名,Encoding.Default);
writer.Formatting=格式化.缩进;
LoadXml(sr.ReadLine());
保存文档(编写器);
writer.Close();
}

谢谢,Ben。我使用Encoding.Default(上面第三行缺少编码值)尝试了这段代码。我得到了一个设计时错误:“参数类型'System.Xml.XmlTextWriter'不可分配给参数类型'System.IO.Stream.”有什么建议吗?