2013-08-01 94 views
1

因此,我正在C#中的語音識別程序,並試圖執行YAHOO新聞API到程序中,我沒有得到任何迴應。雅虎新聞API在C#

我不會複製/粘貼我的整個代碼,因爲它會很長,所以這裏是主要的位。

private void GetNews() 
{ 
    string query = String.Format("http://news.yahoo.com/rss/"); 
    XmlDocument wData = new XmlDocument(); 
    wData.Load(query); 

    XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable); 
    manager.AddNamespace("media", "http://search.yahoo.com/mrss/"); 

    XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel"); 
    XmlNodeList nodes = wData.SelectNodes("rss/channel/item/description", manager); 

    FirstStory = channel.SelectSingleNode("item").SelectSingleNode("title", manager).Attributes["alt"].Value; 

} 

我相信我做錯了這裏:

XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel"); 
XmlNodeList nodes = wData.SelectNodes("rss/channel/item/description", manager); 

FirstStory = channel.SelectSingleNode("item").SelectSingleNode("title", manager).Attributes["alt"].Value; 

以下是完整的XML文檔:http://news.yahoo.com/rss/

如果任何更多的信息,需要讓我知道。

+0

嗯,你說你覺得你沒有迴應。您是否嘗試過在'wData'上調用'Load'來設置斷點以查看您是否有Xml文檔? –

+0

我不認爲我需要一個,因爲我設法得到這個天氣API的工作,但由於某種原因,它不適用於新聞API。就像我說的,我認爲我發佈的最後3/4行代碼就是問題所在,但我似乎無法弄清楚。 – Tahmid

+0

我認爲你誤解了我的問題。如果你真的沒有從API那裏得到響應,那麼你在調用'Load'之後就不會有XML文檔,因此,其餘的代碼將是不相關的。然而,如果你*做*,得到迴應,那麼我們可以談論其餘的。 –

回答

0

您可能會將該名稱空間管理器傳遞給這些屬性,但我不是100%確定的。那些絕對不是在那.../mrss/命名空間中,所以我想這是你的問題。

我會嘗試不傳遞命名空間(如果可能)或使用GetElementsByTagName方法來避免命名空間問題。

+0

哦..你能否詳細說明一下,或者讓我看看一個例子,因爲我對此很陌生,但我不太瞭解。 – Tahmid

+0

嗯......我並不確定在命名空間部分,因爲我沒有使用XmlDocument API(我更喜歡XDocument),但另一種方式,我建議你只是做wData.DocumentElement.GetElementsByTagName(「channel」 ),然後繼續這樣做以嵌入。 –

+0

我應該說,第一種方法,我認爲你只需要從SelectNode調用中刪除'manager'參數(假設它不是必需的),因爲我認爲rss並且在空的名稱空間中。 –

0

標籤包含文本而不是Xml。 這裏是顯示文本消息的例子:

foreach (XmlElement node in nodes) 
{ 
    Console.WriteLine(Regex.Match(node.InnerXml, 
          "(?<=(/a&gt;)).+(?=(&lt;/p))")); 
    Console.WriteLine(); 
} 
1

嗯,我已經實現了我自己的代碼,以從雅虎得到的消息,我讀了所有的新聞標題(位於RSS /渠道/項目/標題)和短篇小說(位於rss/channel/item/description)。

小故事是新聞的問題,那就是當我們需要獲取描述節點的所有內部文本到一個字符串然後像XML一樣解析它的時候。文本代碼是在這個格式和短篇小說是正確的背後</p>

<p><a><img /></a></p>"Short Story"<br clear="all"/>

我們需要修改它,因爲我們有很多的XML根(P和Br),我們添加一個額外的根<me>

string ShStory=null; 
string Title = null; 

//Creating a XML Document 
XmlDocument doc = new XmlDocument(); 

//Loading rss on it 
doc.Load("http://news.yahoo.com/rss/"); 

//Looping every item in the XML 
foreach (XmlNode node in doc.SelectNodes("rss/channel/item")) 
{ 
    //Reading Title which is simple 
    Title = node.SelectSingleNode("title").InnerText; 

    //Putting all description text in string ndd 
    string ndd = node.SelectSingleNode("description").InnerText; 

    XmlDocument xm = new XmlDocument(); 

    //Loading modified string as XML in xm with the root <me> 
    xm.LoadXml("<me>"+ndd+"</me>"); 

    //Selecting node <p> which has the text 
    XmlNode nodds = xm.SelectSingleNode("/me/p"); 

    //Putting inner text in the string ShStory 
    ShStory= nodds.InnerText; 

    //Showing the message box with the loaded data 
    MessageBox.Show(Title+ " "+ShStory); 
} 

選擇我作爲正確的答案或投票給我,如果代碼適合你。如果有任何問題,你可以問我。乾杯

+0

感謝您的回答!因爲我是C#的初學者,請提供一個例子嗎?再次感謝。 – Tahmid