代码之家  ›  专栏  ›  技术社区  ›  David Veeneman

是否为保存的流程文档的XAML换行和缩进?

  •  1
  • David Veeneman  · 技术社区  · 14 年前

    是否有一种方法来格式化保存流程文档时生成的XAML?目前,它都是在一行中运行的,我想用换行和缩进将其分解为元素,使其更易于阅读。

    以下是用于将WPF RichTextBox中的流文档保存为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);
    }
    

    保存工作正常,但正如我所提到的,生成的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>
    

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

    1 回复  |  直到 11 年前
        1
  •  0
  •   Timo Rev    11 年前

    尝试使用 XmlTextWriter :

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

    第二次尝试:

    这似乎产生了一个格式化的XML文档。

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