简体   繁体   English

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

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

I'm trying to format and get the image, text etc.. to give a good look and feel. 我正在尝试格式化并获取图像,文本等。以提供良好的外观。

The XML is something like this: XML是这样的:

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

And the content is: 内容是:

<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>        

At first what I will get is the image or example: he is in content:encoded/p/a/img/src 首先,我将得到的图像或示例:他在content:encoded / p / a / img / src

the code that I try is: 我尝试的代码是:

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

The Content = i.Element(xmnsContent + "encoded").Value give me all the content without formatting something like this: Content = i.Element(xmnsContent +“ encoded”)。Value给我所有内容,而没有格式化如下内容: 在此处输入图片说明

And for exctract the image or some other element from the CData I get a error. 为了从CData中提取图像或其他元素,我得到了一个错误。 Image = i.Element(xmnsContent + "encoded").XPathSelectElement("//p//a//img[@src]").Value is giving error. 图片= i.Element(xmnsContent +“ encoded”)。XPathSelectElement(“ // p // a // img [@src]”)。值给出错误。

I tried this way too, but giving the same error. 我也尝试过这种方法,但是给出了同样的错误。

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

Thanks for all and greetings!! 谢谢大家的问候!!

Finally with a FlowDocumentScrollViewer an the XmlToXamlConverter I get the solution: 最后,使用FlowDocumentScrollViewer和XmlToXamlConverter,我得到了解决方案:

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

After we need to add a Converter like: 之后,我们需要添加一个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();
    }
}

Finally we will add the XmlToXamlConverter from here and another from here . 最后,我们将从此处添加XmlToXamlConverter,并从此处添加另一个。

Another helpfull article for me was this . 我的另一个有益的文章是这样

Greetings! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM