2010-06-10 2 views
5

System.ServiceModel.Syndication.SyndicationFeed 인스턴스에서 어떤 유형의 SyndicationFeed를 읽을 수 있습니까? 내가 가지고있는 모든 것이 URL (blahblah.com/feed) 인 경우 rss 또는 atom 일 수 있으며 유형에 따라 한 가지 또는 다른 것을하고 싶습니다..NET SyndicationFeed (RSS vs Atom)의 피드 유형을 결정하는 방법은 무엇입니까?

문서를 구문 분석하지 않고 특정 문자를 찾는 간단한 방법이 있습니까?

답변

10

이전 질문이지만 대답이 필요합니다.

여기 입니다. RSS 또는 Atom 피드가 있는지 확인하는 비교적 간단한 방법입니다. 그것은 독서, 또는 문서를 읽으려고합니다.

public SyndicationFeed GetSyndicationFeedData(string urlFeedLocation) 
{ 
    XmlReaderSettings settings = new XmlReaderSettings 
     { 
      IgnoreWhitespace = true, 
      CheckCharacters = true, 
      CloseInput = true, 
      IgnoreComments = true, 
      IgnoreProcessingInstructions = true, 
      //DtdProcessing = DtdProcessing.Prohibit // .NET 4.0 option 
     }; 

    if (String.IsNullOrEmpty(urlFeedLocation)) 
     return null; 

    using (XmlReader reader = XmlReader.Create(urlFeedLocation, settings)) 
    { 
     if (reader.ReadState == ReadState.Initial) 
      reader.MoveToContent(); 

     // now try reading... 

     Atom10FeedFormatter atom = new Atom10FeedFormatter(); 
     // try to read it as an atom feed 
     if (atom.CanRead(reader)) 
     { 
      atom.ReadFrom(reader); 
      return atom.Feed; 
     } 

     Rss20FeedFormatter rss = new Rss20FeedFormatter(); 
     // try reading it as an rss feed 
     if (rss.CanRead(reader)) 
     { 
      rss.ReadFrom(reader); 
      return rss.Feed; 
     } 

     // neither? 
     return null; 
    } 
} 
+0

나는 다른 생각을 보았지만 나는 그것을 좋아하지 않는 이유를 기억하지 않는다. 그것은 너무 오래 전이었고,이 작품, 그래서 이것을 최고의 대답, 고맙습니다 :) – SelAromDotNet

+0

그래, 내가이 피드를 시도 할 때 (http://en.espnf1.com/rss/motorsport/story/feeds/0.xml ? type = 2) atom.CanRead (reader) 행이 false를 반환하기 때문에 Atom 2.0 유형의 코드가 작동하지 않습니다. Atom Ver를 처리 할 수있는 해결책은 무엇입니까? 2.0? – Marko

+0

아는 한 원자 2.0이 아닙니다. 그 피드가 깨진 것처럼 보입니다. 그것에는 쓰레기가 있습니다. 이 문제를 해결하려면 피드를 읽으 려하기 전에 피드를 수정하는 것이 좋습니다. 나는 이것을 시도하고 그것은 나를 위해 여기에서 일한다. – Cheeso