2012-05-22 25 views
1

我在SL应用程序中有丰富的文本。我想获得纯文本,所以我将richtext格式转换为xml,然后尝试摆脱所有格式标记。当我使用SL4运行应用程序时可以。但不为沃金5 SLLinq从SL 4和SL获得不同的结果5

这里是丰富格式的数据:

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <Paragraph FontSize="20" FontFamily="Calibri" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" CharacterSpacing="0" Typography.AnnotationAlternates="0" Typography.EastAsianExpertForms="False" Typography.EastAsianLanguage="Normal" Typography.EastAsianWidths="Normal" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.ContextualAlternates="True" Typography.StylisticAlternates="0" 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.Capitals="Normal" Typography.CapitalSpacing="False" Typography.Kerning="True" Typography.CaseSensitiveForms="False" Typography.HistoricalForms="False" Typography.Fraction="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.Variants="Normal" TextOptions.TextHintingMode="Fixed" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Auto" TextAlignment="Left" LineHeight="0" LineStackingStrategy="MaxHeight"> 
    <Run FontSize="20" FontFamily="Calibri" Foreground="#FFFF0000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal">Hello ddd </Run> 
    </Paragraph> 
</Section> 

我使用此代码删除标记(值高于输入数据):

XDocument xmlRichText = XDocument.Parse(value.ToString()); 
    var q = from c in xmlRichText.Descendants(xamlPresentationNamespace + "Run") 
      select (string)c.Attribute("Text"); 
    foreach (string text in q) 
     result += text + " "; 

如何使它适用于SL 4和SL5?

回答

2

我没有看到任何名为“Text”的属性。如果您需要将值“hello DDD”你可以这样做:

XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; 
var path = "...\file.xml"; 

using(var stream = File.Open(path, FileMode.Open)) 
{ 
    var q = XDocument.Load(stream).Descendants(ns + "Run").Select (d => d.Value); 
    var result = string.Join(" ", q); 
    Debug.WriteLine(result); 
} 

这个工作在LINQPad,但我没有在SL4和SL5测试。

+0

谢谢。你的解决方案工作正常。 – KentZhou