XamlReader.Parse(somestring)作为Section - 添加额外的<run> </run>块

时间:2015-08-18 14:53:34

标签: c# xml richtextbox

有人可以帮我这个吗? 我正在从文件中读取xml并尝试发布到RichTextBox。

以下是来自xml的输入:

<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="User" NumberSubstitution.Substitution="AsCulture" FontFamily="Segoe UI" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="12" Foreground="#FFFFFFFF" 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 TextAlignment="Center">
      <Run FontSize="21.75" TextDecorations="Underline">title</Run>
    </Paragraph>
    <Paragraph TextAlignment="Center">
      <Run FontWeight="Bold" FontSize="12.75">Some textSome textSome textSome textSome textSome text</Run>
    </Paragraph>
    <Paragraph TextAlignment="Center">
      <Run FontSize="12.75" TextDecorations="Underline">Some textSome textSome textSome textSome textSome textSome text</Run>
    </Paragraph>
  </Section>

以下是代码:

string strXmlText=null;

        if (page.Content != null)
        {
            using (StringWriter sw = new StringWriter())
            {
                using (System.Xml.XmlTextWriter tx = new System.Xml.XmlTextWriter(sw))
                {
                    page.Content.WriteTo(tx);
                    strXmlText = sw.ToString();
                }
            }

            Section sec = XamlReader.Parse(strXmlText) as Section;

            editor.mainRTB.Document.Blocks.Add(sec);
        } 

在每个运行标记之前和之后(您在上面的xml中看到),正在添加额外的运行标记。我可以看到strXmlText没有额外的Run标签但是在XamlReader.Parse之后它就在那里。

1 个答案:

答案 0 :(得分:0)

更新:似乎问题出在从richtextbox转换为xaml。

使用此代码:

TextRange range = new TextRange(rt.Document.ContentStart, rt.Document.ContentEnd);
MemoryStream stream = new MemoryStream();
range.Save(stream, DataFormats.Xaml);
string xamlText = Encoding.UTF8.GetString(stream.ToArray());
return xamlText;

在检查xamlText字符串时,我看到了额外的Run标记。我认为这是传递给Save ...

的格式

有什么建议吗?

实际上这是我的代码(上面是来自另一个stackoverflow的答案):

private string CreateXamlFromRTB()
    {
        string xamlText = null;
        TextRange tr = new TextRange(editor.mainRTB.Document.ContentStart, editor.mainRTB.Document.ContentEnd);

        MemoryStream ms = new MemoryStream();
        tr.Save(ms, DataFormats.Xaml);
        xamlText = Encoding.ASCII.GetString(ms.ToArray());
            //xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());

        return xamlText;
    }

更新#2: xamlText字符串被传递给一个函数,该函数将它转换为一个XElement,存储在xml中...就像这样(其中content是一个字符串):

newXml.Add(new XElement(XName.Get("Content"), XElement.Parse(content)));

内容看起来像......上面的xml ......