我有以下被转换为字符串的xml文件,我想解析这个xml文件--只返回具有文件扩展名的url
<?xml version="1.0" encoding="utf-8"?>
<channel>
<generator>GOA Celebrations</generator>
<generator>GOA Celebrations</generator>
<pubDate>13 Jan 2016</pubDate>
<title>GOA Celebrations</title>
<link>http://goframe.com.au/rss/herston-inbound</link>
<language>en</language>
<image>
<url>http://goafame.com.au/site/templates/img/logo.png</url>
</image>
<item>
<title>Herston Inbound - Wednesday, 13 Jan - Alex Peisker - 2015-12-21 09:26am - Live</title>
<description></description>
<pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
<link>http://goafame.com.au/gallery/entry/2991/</link>
<enclosure url="http://goafame.com.au/site/assets/files/2991/final_cropped_shutterstock_114908098.jpg" length="" type="" />
<guid isPermaLink="false">c5c1cb0bebd56ae38817b251ad72bedb</guid>
</item>
<item>
<title>Dog 1</title>
<description></description>
<pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
<link>http://goafame.com.au/gallery/entry/2991/</link>
<enclosure url="http://animaliaz-life.com/data_images/dog/dog4.jpg" length="" type="" />
</item>
<item>
<title>Dog 2</title>
<description></description>
<pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
<link>http://goafame.com.au/gallery/entry/2991/</link>
<enclosure url="http://cdn1.theodysseyonline.com/files/2015/12/21/6358631429926013411708851658_Dog-Pictures.jpg" length="" type="" />
</item>
<item>
<title>Dog 3</title>
<description></description>
<pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
<link>http://goafame.com.au/gallery/entry/2991/</link>
<enclosure url="https://i.ytimg.com/vi/AkcfB3z0_-0/maxresdefault.jpg" length="" type="" />
</item>
</channel>我的问题是如何用
规则外泄
期望的输出将是
http://goafame.com.au/site/templates/img/logo.png 114908098.jpg images/dog/dog4.jpg Dog-Pictures.jpg -0/maxresdefault.jpg
以下是我到目前为止所做的尝试
Regex linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string rawString = doc.ToString();
int posCounter = 0;
foreach (Match m in linkParser.Matches(rawString))
{
posCounter++;
links.Add(new LinkModel
{
IsSelected = false,
XmlLink = m.Value,
NodePosition = posCounter
});
}注释: XML文件可以来自任何来源,而且其他一些url不位于链接中,element.Some甚至是嵌套的。这就是为什么我认为使用RegEx而不是XDocument。
发布于 2016-03-04 08:18:32
我的图案可以和你的样本相匹配。在这里测试http://regexstorm.net/tester
https?://[^\s<"]+/[^\s<"]+(?:\.\w{3,4})这样做的目的是找到所有具有一个启动字符(/)的链接,然后是一个文件名模式(以3,4个字符的扩展名结尾)。
发布于 2016-03-04 07:45:52
var allLinkValues = XDocument.Parse(doc.ToString())
.Root
.Elements("item")
.Select(itemElement => itemElement.Element("link").Value)
.ToList();这里
XDocument.Parse(doc.ToString())加载文档。Root指向根元素,然后选择所有"item“元素并选择"link”元素的值。
Linq2Xml的力量!
XPath,XmlDocument是你的其他选择。
通常,如果字符串设计良好,(XML、JSON、RDF等)不要选择RegEx作为第一个选项。对于这些类型的文档,有定义良好的解析器。
上面的查询将使您开始使用Xml导航。
https://stackoverflow.com/questions/35790487
复制相似问题