gpt4 book ai didi

c# - 从 XML CDATA 获取 img src

转载 作者:可可西里 更新时间:2023-11-01 10:27:37 25 4
gpt4 key购买 nike

我是 C# 和 Windows Phone 开发的新手,所以如果我遗漏了显而易见的地方,请原谅我:

我想显示来自位于 http://blog.dota2.com/feed/ 的 RSS XML 提要的缩略图.该图像位于用 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 在我的阅读器应用程序中显示图像。

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

有人帮忙吗?提前致谢,汤姆

最佳答案

假设您的 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 的路径才能正常工作,如果它在一个组(又名数组)中,那么它会更加复杂。从我的代码中,您可以看到如何分离出数组(参见 paras):

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,正如其他人所回答的那样。您可以使用 Get("content:emcoded"...)

返回的 html

关于c# - 从 XML CDATA 获取 img src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10089789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com