从XML CDATA获取img src

时间:2012-04-10 13:30:18

标签: c# xml regex windows cdata

我是C#和Windows Phone开发的新手,请原谅我,如果我错过了显而易见的事情:

我想在位于http://blog.dota2.com/feed/的RSS XML Feed中显示缩略图。该图像位于用HTML编写的CDATA标记内。这是XML代码:

    <content:encoded>
<![CDATA[
<p>We celebrate Happy Bear Pun Week a day earlier as Lone Druid joins Dota 2&#8242;s cast of heroes.</p> <p><a href="http://media.steampowered.com/apps/dota2/posts/LoneDruid_full.jpg "><img class="alignnone" title="The irony is that he's allergic to fur." src="http://media.steampowered.com/apps/dota2/posts/LoneDruid_small.jpg" alt="The irony is that he's allergic to fur." width="551" height="223" /></a></p> <p>Community things:</p> <ul> <li><a href="http://www.itsgosu.com/game/dota2/articles/ig-monthly-madness-invitational-finals-mar-29_407" target="_blank">It&#8217;s Gosu&#8217;s Monthly Madness</a> tournament finals are tomorrow, March 29th. You don&#8217;t want to miss this, we hear it could be more than we can bear.</li> <li>Bear witness to <a href="http://www.team-dignitas.net/articles/blogs/DotA/1092/Dota-2-Ultimate-Guide-to-Warding/" target="_blank">Team Dignitas&#8217; Ultimate Guide to Warding</a>. This should be required teaching in clawsrooms across the globe.</li> <li>Great Explorer Nullf has <a href="http://nullf.deviantart.com/#/d4ubxiu" target="_blank">compiled the eating habits</a> of the legendary Tidehunter in one handy chart. This might give you paws before deciding to head to the beach.</li> </ul> <p>Bear in mind that there will not be an update next week as we will be hibernating during that time.</p> <p>Today&#8217;s bearlog is available <a href="http://store.steampowered.com/news/7662" target="_blank">here</a>.</p> <p>&nbsp;</p> <p>Bear.</p>
]]>
</content:encoded>

我只需要 <img src="http://media.steampowered.com/apps/dota2/posts/LoneDruid_small.jpg" /> 所以我可以使用URL在我的阅读器应用程序中显示图像。

我听说有人说不使用正则表达式,因为解析HTML是不好的做法。我创建这个作为概念证明,不需要担心这个。我正在寻找获取图像URL的最快方法,然后在我的应用程序中调用它。

有没有人有任何帮助? 提前致谢, 汤姆

3 个答案:

答案 0 :(得分:1)

您可以在准备好使用HtmlAgilityPack

时尝试此操作
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(yourstring);
var imgLinks = doc.DocumentNode
    .Descendants("img")
    .Select(n => n.Attributes["src"].Value)
    .ToArray();

答案 1 :(得分:1)

假设你的xml看起来像这样(我确定它没有),以及这些扩展:http://searisen.com/xmllib/extensions.wiki

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:content='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'>
  <content:encoded>
    <![CDATA[
<p>We celebrate ...</p> 
<p>
  <a href="http://media.steampowered.com/apps/dota2/posts/LoneDruid_full.jpg ">
    <img class="alignnone" title="The irony is that he's allergic to fur." 
        src="http://media.steampowered.com/apps/dota2/posts/LoneDruid_small.jpg" />
  </a>
</p> 
<p>the rest removed</p> 
]]>
  </content:encoded>
</root>

这将从第二段获得图像源 - 硬编码和丑陋,但这就是你需要的所有内容。您必须提供path/to/content:encoded的路径才能使其工作,如果它在一个组(也就是数组)中,那么它将更加复杂。从我的代码中你可以看到如何分离出数组(参见参考文献):

XElement root = XElement.Load(file) // or .Parse(string)
string html = root.Get("content:encoded", string.Empty).Replace("&nbsp", " ");
XElement xdata = XElement.Parse(string.Format("<root>{0}</root>", html));
XElement[] paras = xdata.GetElements("p").ToArray();
string src = paras[1].Get("a/img/src", string.Empty);

PS 这是有效的,因为HTML是正确形成的,如果不是,那么你将不得不使用HtmlAgilityPack,因为其他人已经回答了。您可以使用html

返回的Get("content:emcoded" ...)

答案 2 :(得分:0)

const string pattern = @"<img.+?src.*?\=.*?""(<?URL>.*?)""";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
var match = regex.Match(myCDataText);
var domain = match.Groups["URL"].Value;