LINQ query problem

后端 未结 4 686
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 01:31

Can\'t get any result in feeds. feedXML has the correct data.

XDocument feedXML = XDocument.Load(@\"http://search.twitter.com/search.atom?q=twitter\");

var feed         


        
4条回答
  •  忘掉有多难
    2021-01-27 02:04

    If you look at the XML returned by the HTTP request, you will see that it has an XML namespace defined:

    
    
      tag:search.twitter.com,2005:search/twitter
      ...
    
    

    XML is just like C#, if you use an element name with the wrong namespace, it is not considered to be the same element! You need to add the required namepsace to your query:

    private static string AtomNamespace = "http://www.w3.org/2005/Atom";
    
    public static XName Entry = XName.Get("entry", AtomNamespace);
    
    public static XName Published = XName.Get("published", AtomNamespace);
    
    public static XName Title = XName.Get("title", AtomNamespace);
    
    var items = doc.Descendants(AtomConst.Entry)
                    .Select(entryElement => new FeedItemViewModel()
                    new {
                      Title = entryElement.Descendants(AtomConst.Title).Single().Value,
                      ...
                    });
    

提交回复
热议问题