2011-04-02 63 views
0
string url = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=fosil+fuel"; 
List<Resource> sites = new List<Resource>(); 
WebRequest request = WebRequest.Create(url); 
using (WebResponse response = request.GetResponse()) 
{ 
     // Get the response stream 
     StreamReader reader = new StreamReader(response.GetResponseStream()); 
     XDocument xmlDoc = new XDocument(); 
     xmlDoc = XDocument.Parse(reader.ReadToEnd()); 
     var results = from q in xmlDoc.Descendants("Result") 
        select new 
        { 
          Url = q.Element("ClickUrl").Value, 
          Title = q.Element("Title").Value, 
          Date = q.Element("ModificationDate").Value, 
        }; 
     foreach (var item in results) 
     { 
      sites.Add(new Resource(item.Title, item.Date, item.Url)); 
     } 
} 

雅虎的XML的样子:C#的LINQ to XML中是与

<ResultSet xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/WebSearchService/V1/WebSearchResponse.xsd" type="web" totalResultsAvailable="134000" totalResultsReturned="10" firstResultPosition="1" moreSearch="/WebSearchService/V1/webSearch?query=fosil+fuel&appid=YahooDemo&region=us"> 
    <Result> 
     <Title>Fosil Fuel | Flickr - Photo Sharing!</Title> 
     <Summary>Fosil Fuel ... &lt;a href="http://www.flickr.com/photos/thomashawk/3910488337/" title="Fosil Fuel by Thomas Hawk, on Flickr"&gt;&lt;img src="http://farm3.static.flickr. ...</Summary> 
     <Url>http://www.flickr.com/photos/thomashawk/3910488337/</Url> 
     <ClickUrl>http://www.flickr.com/photos/thomashawk/3910488337/</ClickUrl> 
     <DisplayUrl>www.flickr.com/photos/thomashawk/3910488337/</DisplayUrl> 
     <ModificationDate>1298707200</ModificationDate> 
     <MimeType>text/html</MimeType> 
     <Cache> 
      <Url>http://uk.wrs.yahoo.com/_ylt=A0WTeekRk5dNbBEAx4zdmMwF;_ylu=X3oDMTBwZTdwbWtkBGNvbG8DZQRwb3MDMQRzZWMDc3IEdnRpZAM-/SIG=16k5dp83c/EXP=1301865617/**http%3A//66.218.69.11/search/cache%3Fei=UTF-8%26appid=YahooDemo%26query=fosil%2Bfuel%26u=www.flickr.com/photos/thomashawk/3910488337/%26w=fosil%2Bfuel%2Bfuels%26d=V6aQZ_bJWYzN%26icp=1%26.intl=us</Url> 
      <Size>119332</Size> 
      </Cache> 
     </Result> 
    </ResultSet> 

我在做什么错?每次我尝试新的东西时,调试器都会提示我的结果是空的。

回答

1

你需要指定一个命名空间: xmlDoc.Descendants(XName.Get( “结果”, “瓮:雅虎:检索”))

您可以简化您的代码位:

 var url = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=fosil+fuel"; 
     var request = WebRequest.Create(url); 
     using (var response = request.GetResponse()) 
     { 
      // Get the response stream 
      var xmlDoc = XDocument.Load(response.GetResponseStream()); 
      var results = from q in xmlDoc.Descendants(XName.Get("Result", "urn:yahoo:srch")) 
          select new Resource(
           q.Element(XName.Get("Title", "urn:yahoo:srch").Value, 
           q.Element(XName.Get("ModificationDate", "urn:yahoo:srch").Value, 
           q.Element(XName.Get("ClickUrl", "urn:yahoo:srch").Value); 
      return results.ToList(); 
     } 
+0

我试过这个以及为下面的每个元素设置XName。然而,它不断从linq查询中获取null .... – Jarod 2011-04-03 03:46:41

+0

我复制了你的代码并运行它。添加命名空间给了我结果。 https://gist.github.com/900198 – Talljoe 2011-04-03 04:52:56

0

问题在于“结果”具有名称空间“urn:yahoo:srch”,您正在省略。尝试xmlDoc.Descendants(XName.Get("Result", "urn:yahoo:srch")),也将努力

0

假设你不需要设立代理或凭据WebRequest可以简化居然还比Talljoe实际上提到:

string url = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=fosil+fuel"; 
Func<string, XName> qualifiedName = name => XName.Get(name, "urn:yahoo:srch"); 
XDocument xmlDoc = XDocument.Load(url); 
var results = from q in xmlDoc.Descendants(qualifiedName("Result")) 
       select new Resource(
        q.Element(qualifiedName("ClickUrl")).Value, 
        q.Element(qualifiedName("Title")).Value, 
        q.Element(qualifiedName("ModificationDate")).Value); 
return results.ToList(); 

编辑 - 修正了我的原始响应(缺少用于访问元素的XName)并添加了一个时髦的Func<>以重新使用名称空间限定。适用于我的机器(TM)。