【发布时间】:2014-03-12 18:41:22
【问题描述】:
我正在尝试格式化并获取图像、文本等。以提供良好的外观和感觉。
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 中提取图像或其他元素,我得到一个错误。 Image = i.Element(xmnsContent + "encoded").XPathSelectElement("//p//a//img[@src]").Value 给出错误.
我也试过这个方法,但报同样的错误。
Image = i.Element(xmnsContent+"encoded").Element("p").Element("a").Element("img").Attribute("src").Value
谢谢大家的问候!!
【问题讨论】:
-
没有人可以帮助我吗? :(