2016-03-04 50 views
1

我有一個轉換成字符串 下面的XML文件我想解析XML文件只返回URL文件擴展正則表達式解析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> 

我的問題是如何做到這一點使用

定期Exression

所需的輸出是

http://goafame.com.au/site/templates/img/logo.png http://goafame.com.au/site/assets/files/2991/final_cropped_shutterstock_114908098.jpg http://animaliaz-life.com/data_images/dog/dog4.jpg http://cdn1.theodysseyonline.com/files/2015/12/21/6358631429926013411708851658_Dog-Pictures.jpg

https://i.ytimg.com/vi/AkcfB3z0_-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不在鏈接元素中。有些甚至是嵌套的。這就是爲什麼我想使用RegEx而不是XDocument。

+5

爲什麼你需要正則表達式?使用Xpath,它將通過一行代碼:) –

+1

不要對此使用RegEx。只需使用一個XML解析器。例如XDocument。 –

+0

恕我直言,使用正則表達式解析XML不是一個好主意。我會去DOM或SAX。 –

回答

2

我的圖案可以匹配您的樣本。這裏http://regexstorm.net/tester

https?://[^\s<"]+/[^\s<"]+(?:\.\w{3,4}) 

的想法進行測試,尋找跟一個文件名模式(一端與3,4個字符擴展名)具有防濺字符(/)的所有鏈接。

2
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導航。

+0

謝謝你的回答,但我這裏的問題是xml是動態的,它可能來自任何rss提要,並且一些鏈接不在鏈接元素 –

+1

好。那麼正則表達式是有道理的。 –

相關問題