Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 957355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:41:17+00:00 2026-05-16T00:41:17+00:00

Is there a way to format the XAML that is generated when a FlowDocument

  • 0

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="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>

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:41:17+00:00Added an answer on May 16, 2026 at 12:41 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer BorderContainer has a minimum size of 112 pixels wide by… May 16, 2026 at 9:38 pm
  • Editorial Team
    Editorial Team added an answer If you apply it to integral types, they are bitwise… May 16, 2026 at 9:38 pm
  • Editorial Team
    Editorial Team added an answer Since it took a while for me to get an… May 16, 2026 at 9:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there an easy way to transform or format a string as part of
Is there a way I can format for example: 0000000000 into (000)000-0000? I'm returning
Is there any way to get exact below format of current date- 18 Aug
Is there any way to make DateFormat format a date with a full year
Is there an out of the box way to format in python (or within
Or, to be more clear, how can I format a block of text (in
A templated control I'm working on uses a ValueConverter like so: <ListBox> <ListBox.Resources> <Controls:CodeDescriptionValueConverter
I am doing something like this in a Silverlight 3 datagrid: for (int x
I'm building simple dictionary application using WPF. I'm using MVVM pattern, databinding and FlowDocument
i'm new to wpf and xaml. i find solutions of most of the problems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.