System.ServiceModel.Syndication.SyndicationFeedインスタンスから、読み取られている SyndicationFeed のタイプを特定することはできますか? 私が持っているのが url (blahblah.com/feed) だけである場合、それは rss または atom である可能性があり、タイプに応じて、どちらかを実行したいと考えています。
ドキュメントを解析せずに特定の文字を探す簡単な方法はありますか?
System.ServiceModel.Syndication.SyndicationFeedインスタンスから、読み取られている SyndicationFeed のタイプを特定することはできますか? 私が持っているのが url (blahblah.com/feed) だけである場合、それは rss または atom である可能性があり、タイプに応じて、どちらかを実行したいと考えています。
ドキュメントを解析せずに特定の文字を探す簡単な方法はありますか?
古い質問ですが、回答に値します。
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;
}
}