C# 从XML CDATA获取img src
我是C#和Windows Phone开发的新手,因此如果我错过了显而易见的东西,请原谅我: 我想显示位于的RSS XML提要中的缩略图。图像位于用HTML编写的CDATA标记中。以下是XML代码:C# 从XML CDATA获取img src,c#,xml,regex,windows,cdata,C#,Xml,Regex,Windows,Cdata,我是C#和Windows Phone开发的新手,因此如果我错过了显而易见的东西,请原谅我: 我想显示位于的RSS XML提要中的缩略图。图像位于用HTML编写的CDATA标记中。以下是XML代码: <content:encoded> <![CDATA[ <p>We celebrate Happy Bear Pun Week a day earlier as Lone Druid joins Dota 2′s cast of heroes.
<content:encoded>
<![CDATA[
<p>We celebrate Happy Bear Pun Week a day earlier as Lone Druid joins Dota 2′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’s Gosu’s Monthly Madness</a> tournament finals are tomorrow, March 29th. You don’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’ 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’s bearlog is available <a href="http://store.steampowered.com/news/7662" target="_blank">here</a>.</p> <p> </p> <p>Bear.</p>
]]>
</content:encoded>
我们提前一天庆祝快乐熊双关周,因为孤独的德鲁伊加入了Dota 2′;我们的英雄阵容。社区事物:- 明天,3月29日是锦标赛决赛。你不’;I don’我不想错过这个机会,我们听说这可能超出了我们的承受能力。这应该是在全球各地的爪子屋进行教学的必要条件。
- 伟大的探险家纳尔夫在一张方便的图表中列出了传说中的潮人。在你决定去海滩之前,这可能会给你一个爪子。
请记住,下周不会有更新,因为我们将在这段时间冬眠。今天&8217;s bearlog可用。熊
]]>
我只需要这个
因此,我可以使用URL在我的reader应用程序中显示图像
我听到有人说不要使用正则表达式,因为这是解析HTML的糟糕做法。我创建这个是为了证明概念,不需要担心这个。我正在寻找最快的方法来获取该图像的URL,然后在我的应用程序中调用它
有人帮忙吗?
提前感谢,,
Tom您可以在准备好使用时尝试此功能
const字符串模式=@“*?)”;
Regex Regex=new Regex(模式,RegexOptions.IgnoreCase);
var match=regex.match(MyCDATA文本);
var domain=match.Groups[“URL”].Value;
假设您的xml看起来像这样(我肯定不是这样),并且这些扩展:
PS这是因为HTML格式正确,如果格式不正确,则必须像其他人回答的那样使用HtmlAgilityPack。您可以使用从
Get(“content:emcoded”…)
返回的html
,只需解析xml,就可以轻松得多!有没有关于最好的方法的建议?:)如果你坚持使用正则表达式,试试这个-
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(yourstring);
var imgLinks = doc.DocumentNode
.Descendants("img")
.Select(n => n.Attributes["src"].Value)
.ToArray();
const string pattern = @"<img.+?src.*?\=.*?""(<?URL>.*?)""";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
var match = regex.Match(myCDataText);
var domain = match.Groups["URL"].Value;
<?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>
XElement root = XElement.Load(file) // or .Parse(string)
string html = root.Get("content:encoded", string.Empty).Replace(" ", " ");
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);