在C#中解析论坛的RSS输出以满足我的需求

时间:2014-01-08 06:15:23

标签: c# php regex rss

我想根据我的需要解析以下RSS输出: http://forum.ragezone.com/external.php?type=RSS2&forumids=427

但是,我并不熟悉Regex(我认为在这种情况下需要它)。 我有以下代码将整个输出下载为字符串:

 WebClient wc = new WebClient();
        Stream st = wc.OpenRead(textBox1.Text);

        using (StreamReader sr = new StreamReader(st))
        {
            Output = sr.ReadToEnd();
        }

但是,我不确定如何解析它。基本上,每个“项目” - 这是一个线程,就像这样:

    <item>
<title>
<![CDATA[ [Release] [Add-on] Choice Dialog Easy Selections ]]>
</title>
<link>
http://forum.ragezone.com/f427/add-choice-dialog-easy-selections-979948-new/
</link>
<pubDate>Tue, 07 Jan 2014 16:04:20 GMT</pubDate>
<description>
<![CDATA[
*Note:* This won't affect your current scripts or harm them, but you may add it to whatever you...
]]>
</description>
<content:encoded>
<![CDATA[
<div><b>Note:</b> This won't affect your current scripts or harm them, but you may add it to whatever you want.<br /> <b>Example of usage: </b><br /> <br /> Before:<br /> <div class="bbcode_container">
 <div class="bbcode_description">PHP Code:</div>
 <hr /><code class="bbcode_code"><code><span style="color: #000000"> <span style="color: #0000BB">cm</span><span style="color: #007700">.</span><span style="color: #0000BB">sendSimple</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hey!&nbsp;\r\nPlease&nbsp;select&nbsp;an&nbsp;option.#b\r\n#L0#Option&nbsp;1.#l\r\n#L1#Option&nbsp;2.#l\r\n#L2#Option&nbsp;3.#l"</span><span style="color: #007700">);&nbsp;
<br /></span><span style="color: #0000BB"></span> </span> </code></code><hr />
 </div>After:<br /> <div class="bbcode_container">
 <div class="bbcode_description">PHP Code:</div>
 <hr /><code class="bbcode_code"><code><span style="color: #000000"> <span style="color: #0000BB">cm</span><span style="color: #007700">.</span><span style="color: #0000BB">sendSimple</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hey!&nbsp;\r\nPlease&nbsp;select&nbsp;an&nbsp;option."</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Option&nbsp;1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Option&nbsp;2"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Option&nbsp;3"</span><span style="color: #007700">);&nbsp;
<br /></span><span style="color: #0000BB"></span> </span> </code></code><hr />
 </div><b>How to add:</b>​<br /> <br /> Navigate to NPCConversationManager, replace your current sendSimple method with the following:<br /> <br /> <div class="bbcode_container">
 <div class="bbcode_description">PHP Code:</div>
 <hr /><code class="bbcode_code"><code><span style="color: #000000"> <span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">void&nbsp;sendSimple</span><span style="color: #007700">(</span><span style="color: #0000BB">String&nbsp;text</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">String</span><span style="color: #007700">...&nbsp;</span><span style="color: #0000BB">selections</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">selections</span><span style="color: #007700">.</span><span style="color: #0000BB">length&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;</span><span style="color: #FF8000">//&nbsp;Adding&nbsp;this&nbsp;even&nbsp;if&nbsp;selections&nbsp;length&nbsp;is&nbsp;0&nbsp;will&nbsp;do&nbsp;anything,&nbsp;but&nbsp;whatever.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">text&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #DD0000">"#b\r\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">int&nbsp;i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">i</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">selections</span><span style="color: #007700">.</span><span style="color: #0000BB">length</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">text&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #DD0000">"#L"&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"#"&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">selections</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">i</span><span style="color: #007700">&#93;&nbsp;+&nbsp;</span><span style="color: #DD0000">"#l\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sendSimple</span><span style="color: #007700">(</span><span style="color: #0000BB">text</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;
<br /></span><span style="color: #0000BB"></span> </span> </code></code><hr />
 </div></div>
]]>
</content:encoded>
<category domain="http://forum.ragezone.com/f427/">Releases</category>
<dc:creator>Fraysa</dc:creator>
<guid isPermaLink="true">
http://forum.ragezone.com/f427/add-choice-dialog-easy-selections-979948/
</guid>
</item>

我希望能够检索每个项目的标题,链接,内容和创建者。 例如,我发布的项目:它的标题是“[Release] [Add-on] Choice Dialog Easy Selections”,内容如下:

<content:encoded>

创作者是:

<dc:creator>Fraysa</dc:creator>

我该怎么做?我在搞乱Regex方面遇到了真正的问题。我试图分开它,但搞砸了。有人可以帮忙吗? 最好的问候

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

String lines="<title><![CDATA[ [Release] [Add-on] Choice Dialog Easy Selections ]]></title>";
String REGEX="(?<=<title><\\!\\[CDATA\\[)(.*)(?=\\]\\]></title>)";
Pattern pattern=Pattern.compile(REGEX);
Matcher matcher =pattern.matcher(lines);
if(matcher.find()){
    System.out.println("Title  : "+matcher.group());
}

我使用了lookbehind (?<=)并向前看(?=)个表达式。