开发者

Line breaks and indenting for the XAML of a saved FlowDocument?

Is there a way to format the XAML that is generated when a FlowDocument is saved? Currently, it is all run together on one line, and I'd like to break it up into its elements, with line breaks and indenting, to make it a little easier to read.

Here is the code I am using to save a FlowDocument from a WPF RichTextBox as a XAML file:

// 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);
}

The save works fine, but as I mentioned, the XAML that is generated is all run together, with no indentation or line breaks for its various elements:

<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="p开发者_Go百科reserve" 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>

I'd like to have the XAML formatted in the file like conventional XAML, with line breaks and indentation for the various elements. Is there any way to have .NET add the formatting as it generates the XAML? Thanks.


Try using an XmlTextWriter:

using (FileStream fs = File.Create(fileName)
{
    XmlTextWriter writer = new XmlTextWriter(fs, Encoding.Default);
    writer.Formatting = Formatting.Indented;
    textRange.Save(writer, DataFormats.Xaml);
}

Second Attempt:

This seems to produce a formatted XML document.

using (MemoryStream ms = new MemoryStream())
{
    textRange.Save(ms, DataFormats.Xaml);

    ms.Position = 0;
    StreamReader sr = new StreamReader(ms);

    XmlDocument doc = new XmlDocument();
    XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.Default);
    writer.Formatting = Formatting.Indented;

    doc.LoadXml(sr.ReadLine());
    doc.Save(writer);
    writer.Close();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜