2014-03-12 71 views
0

我試圖格式化並獲取圖像,文本等等,以提供良好的外觀和感覺。使用C#中的C#XDocument或XPath從RSS格式化CDATA類型

的XML是這樣的:

xmlns:content="http://purl.org/rss/1.0/modules/content/" 

,內容是:

<content:encoded> 
<![CDATA[ 
<p><a href="http://i2.wp.com/geekytheory.com/wp-content/uploads/2014/03/Screen-Shot-  2013-11-11-at-11.38.50.png"><img class="size-full wp-image-7447 aligncenter" alt="Screen Shot 2013-11-11 at 11.38.50" src="http://i2.wp.com/geekytheory.com/wp- content/uploads/2014/03/Screen-Shot-2013-11-11-at-11.38.5 
]]> 
<![CDATA[ 
0.png?resize=788%2C644" data-recalc-dims="1" /></a></p> <p style="text-align: justify"> 
</p>]]> 
< /content:encoded>   

起初什麼,我會得到的是圖像或例子:他是在內容:編碼/ p/A/IMG/src目錄

的代碼,我嘗試是:

內容= i.Element(xmnsContent +「編碼」)值給我所有的未格式化的內容是這樣的: enter image description here

而對於exctract圖像或從其他元素CData我得到一個錯誤。 Image = i.Element(xmnsContent +「encoded」)。XPathSelectElement(「// p // a // img [@src]」)。值給出錯誤。

我也試過這種方式,但給出了同樣的錯誤。

Image = i.Element(xmnsContent+"encoded").Element("p").Element("a").Element("img").Attribute("src").Value 

謝謝你的問候!

+0

沒有人可以幫助我嗎? :( – soydachi

回答

0
與FlowDocumentScrollViewer

最後一個的XmlToXamlConverter我得到的解決方案:

<FlowDocumentScrollViewer 
    Document="{Binding Content, Converter={StaticResource HtmlToFlowDocConverter}}"/> 

後,我們需要添加一個轉換器,如:

public object Convert(object value, Type targetType, object parameter, 
    CultureInfo culture) 
    { 

     var xaml = HtmlToXamlConverter.ConvertHtmlToXaml((string)value, true); 
     var flowDocument = XamlReader.Parse(xaml); 
     if (flowDocument is FlowDocument) 
      SubscribeToAllHyperlinks((FlowDocument)flowDocument); 
     return flowDocument; 
    } 

    private void SubscribeToAllHyperlinks(FlowDocument flowDocument) 
    { 
     var hyperlinks = GetVisuals(flowDocument).OfType<Hyperlink>(); 
     foreach (var link in hyperlinks) 
      link.RequestNavigate += LinkRequestNavigate; 
    } 

    private static IEnumerable<DependencyObject> GetVisuals(DependencyObject root) 
    { 
     foreach (var child in 
      LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>()) 
     { 
      yield return child; 
      foreach (var descendants in GetVisuals(child)) 
       yield return descendants; 
     } 
    } 

    private void LinkRequestNavigate(object sender, 
     System.Windows.Navigation.RequestNavigateEventArgs e) 
    { 
     Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)); 
     e.Handled = true; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

最後,我們將從here,另一名來自加XmlToXamlConverter here

對我來說,另一篇有用的文章是this

問候!