簡體   English   中英

在WPF中使用C#XDocument或XPath從RSS格式化CDATA類型

[英]Format CDATA Type from RSS with C# XDocument or XPath in WPF

我正在嘗試格式化並獲取圖像,文本等。以提供良好的外觀。

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>        

首先,我將得到的圖像或示例:他在content:encoded / p / a / img / src

我嘗試的代碼是:

private ObservableCollection<RssItem> ParseXmlString(string xmlString)
    {
        XDocument xmlDoc = XDocument.Parse(xmlString);
        XNamespace xmns = @"http://purl.org/dc/elements/1.1/";
        XNamespace xmnsContent = @"http://purl.org/rss/1.0/modules/content/";
        var itemsList = xmlDoc.Descendants("item").Select(i => new RssItem()
        {
            Author = i.Element(xmns + "creator").Value,
            Title = i.Element("title").Value,
            Description = i.Element("description").Value,
            Content = i.Element(xmnsContent + "encoded").Value,
            Image = i.Element(xmnsContent + "encoded").XPathSelectElement("//p//a//img[@src]").Value,
            Date = DateTime.Parse(i.Element("pubDate").Value)
        }).ToList();

        return new ObservableCollection<RssItem>(itemsList);
    }

Content = i.Element(xmnsContent +“ encoded”)。Value給我所有內容,而沒有格式化如下內容: 在此處輸入圖片說明

為了從CData中提取圖像或其他元素,我得到了一個錯誤。 圖片= i.Element(xmnsContent +“ encoded”)。XPathSelectElement(“ // p // a // img [@src]”)。值給出錯誤。

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

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

謝謝大家的問候!!

最后,使用FlowDocumentScrollViewer和XmlToXamlConverter,我得到了解決方案:

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

之后,我們需要添加一個Converter:

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

最后,我們將從此處添加XmlToXamlConverter,並從此處添加另一個。

我的另一個有益的文章是這樣

問候!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM