使用C#XDocument或WPF中的XPath格式化RSS中的CDATA类型

时间:2014-03-12 18:41:23

标签: c# wpf mvvm 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

我尝试的代码是:

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 +&#34;已编码&#34;)。值 为我提供所有内容,而不进行格式化: enter image description here

对于exctract来自CData的图像或其他元素,我得到一个错误。 Image = i.Element(xmnsContent +&#34; encoded&#34;)。XPathSelectElement(&#34; // p // a // img [@src]&#34;)。 正在给出错误。

我也尝试过这种方式,但也给出了同样的错误。

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

感谢所有人和问候!!

1 个答案:

答案 0 :(得分: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

问候!