何となく Blog by Jitta
Microsoft .NET 考

目次

Blog 利用状況
  • 投稿数 - 761
  • 記事 - 18
  • コメント - 35943
  • トラックバック - 222
ニュース
  • IE7以前では、表示がおかしい。div の解釈に問題があるようだ。
    IE8の場合は、「互換」表示を OFF にしてください。
  • 検索エンジンで来られた方へ:
    お望みの情報は見つかりましたか? よろしければ、コメント欄にどのような情報を探していたのか、ご記入ください。
It's ME!
  • はなおか じった
  • 世界遺産の近くに住んでます。
  • Microsoft MVP for Visual Developer ASP/ASP.NET 10, 2004 - 9, 2011
広告

記事カテゴリ

書庫

日記カテゴリ

ギャラリ

その他

わんくま同盟

同郷

 

前回、更新通知に3つのフォーマットの系統があることを確認しました。とりあえず、ルート要素のローカル名を見れば、どの系統なのか判別できそうだとわかりました。次、配信されているものを取り出します。

さて、サンプルを見ると、次のようになっています。


static IEnumerable<XElement> GetItems() {
    string[] feeds = {
        "http://blogs.msdn.com/ericlippert/rss.aspx",
        "http://blogs.msdn.com/wesdyer/rss.aspx",
        "http://blogs.msdn.com/charlie/rss.aspx",
        "http://blogs.msdn.com/cyrusn/rss.aspx",
        "http://blogs.msdn.com/mattwar/rss.aspx",
        "http://blogs.msdn.com/lucabol/rss.aspx",
        "http://www.pluralsight.com/blogs/dbox/rss.aspx",
        "http://blogs.msdn.com/jomo_fisher/rss.aspx"
    };
    foreach (var str in feeds) {
        var feed = XDocument.Load(str);
        var items = feed.Root.Element("channel").Elements("item");
        foreach (var item in items)
            yield return item;
    }
}

肝心の所は、下の foreach ブロック内にある、feed.Root.Element("channel") のところです。ドキュメントを確認します。

XContainer.Element メソッド より

指定した XName の最初の子要素を (ドキュメント順に) 取得します。

つまり、feed が示す XDocument インスタンスからルート要素を取り出し、ルート要素の "channel" という名前のエレメントを取り出す、ということですね。

とりあえず、他のフォーマットでも大丈夫か確認するため、このコードの feed を書き換え、実行します。試しに http://www.atmarkit.co.jp/aboutus/rss/rss.html から、RSS 1.0 のフォーマットを選んでみます。

例外が発生しました。NullReferenceException です。

デバッガで止めて、クイックウォッチを起動します。ここに feed. と入力すると、こんなところまでインテリセンスが働くのですね!続けて feed.Root.Element("channel") と入力します。すると、null が返ってきています。

データを確認します。ブラウザでアクセスし、ソースを表示します。抜粋します。


<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/modules/syndication/" xml:lang="ja">
  <channel rdf:about="http://www.atmarkit.co.jp">
    <title>@IT</title>
    <link>http://www.atmarkit.co.jp</link>
    <description>ITエンジニア対象の技術解説情報&コミュニティサイト</description>
    <language>ja</language>
    <dc:date>2009-06-08T03:00:13+09:00</dc:date>
    <items>

ルート要素である RDF:rdf の直下に channel があります。なぜ取れないのでしょう?

ドキュメントを読み返し、いろいろいじっていると、feed.Root.Nodes() だと、アクセスできることがわかりました。ここから NodeType を見ると XElement となっています。Nodes メソッドが返す XNode インスタンスを XElement にキャストすると、キャストが成功しました。ここからアクセスすることにします。


/// <summary>
/// 指定されたエレメントに定義された、指定された名前を持つ最初のエレメントを取得します。
/// </summary>
/// <param name="rootElement">エレメントを探す元になるエレメント</param>
/// <param name="elementName">検索するエレメントのローカル名</param>
/// <returns>エレメントのノードから最初に見つかったエレメント</returns>
static public XElement GetElementFromNodes(XElement rootElement, string elementName) {
    var nodes = rootElement.Nodes();
    foreach (var node in nodes) {
        if (node.NodeType == System.Xml.XmlNodeType.Element) {
            XElement element = (XElement)node;
            if (element.Name.LocalName == elementName) {
                return element;
            }
        }
    }
    return null;
}
投稿日時 : 2009年6月8日 22:38
コメント
  • # re: XDocument って、RSS 2.0 しか解析してくれないの?!
    aetos
    Posted @ 2009/06/08 23:38
    > なぜ取れないのでしょう?

    名前空間の指定がないからです。
  • # Hello, all is going perfectly here and ofcourse every one is sharing facts, that's genuinely excellent, keep up writing.
    Hello, all is going perfectly here and ofcourse ev
    Posted @ 2018/10/05 7:16
    Hello, all is going perfectly here and ofcourse every one is sharing facts, that's
    genuinely excellent, keep up writing.
  • # Hello there! This blog post could not be written any better! Reading through this post reminds me of my previous roommate! He constantly kept preaching about this. I am going to forward this post to him. Fairly certain he will have a very good read. Many
    Hello there! This blog post could not be written a
    Posted @ 2018/10/07 3:39
    Hello there! This blog post could not be written any better!
    Reading through this post reminds me of my previous roommate!
    He constantly kept preaching about this. I am going to forward this post to him.
    Fairly certain he will have a very good read. Many thanks for sharing!
  • # Ahaa, its good conversation on the topic of this article here at this weblog, I have read all that, so now me also commenting here.
    Ahaa, its good conversation on the topic of this a
    Posted @ 2018/10/19 4:00
    Ahaa, its good conversation on the topic of this
    article here at this weblog, I have read all that, so
    now me also commenting here.
  • # It is not my first time to visit this website, i am visiting this web page dailly and take pleasant facts from here every day.
    It is not my first time to visit this website, i a
    Posted @ 2018/11/03 11:26
    It is not my first time to visit this website, i am visiting this web page dailly and take pleasant facts from here every day.
  • # I am really loving the theme/design of your weblog. Do you ever run into any browser compatibility problems? A small number of my blog visitors have complained about my blog not working correctly in Explorer but looks great in Safari. Do you have any id
    I am really loving the theme/design of your weblog
    Posted @ 2018/11/21 2:08
    I am really loving the theme/design of your weblog.

    Do you ever run into any browser compatibility problems?
    A small number of my blog visitors have complained about
    my blog not working correctly in Explorer but looks great in Safari.
    Do you have any ideas to help fix this issue?
  • # Woah! I'm really loving the template/theme of this blog. It's simple, yet effective. A lot of times it's very hard to get that "perfect balance" between usability and appearance. I must say you've done a amazing job with this. Additionally, th
    Woah! I'm really loving the template/theme of this
    Posted @ 2019/04/01 13:05
    Woah! I'm really loving the template/theme of this blog.
    It's simple, yet effective. A lot of times it's very hard
    to get that "perfect balance" between usability and appearance.
    I must say you've done a amazing job with this.

    Additionally, the blog loads extremely quick
    for me on Opera. Outstanding Blog!
  • # Howdy, I do believe your website could possibly be having internet browser compatibility problems. When I take a look at your website in Safari, it looks fine however, when opening in Internet Explorer, it has some overlapping issues. I merely wanted to
    Howdy, I do believe your website could possibly be
    Posted @ 2019/08/14 15:56
    Howdy, I do believe your website could possibly be having internet
    browser compatibility problems. When I take a look at your
    website in Safari, it looks fine however, when opening in Internet Explorer, it has some overlapping issues.
    I merely wanted to give you a quick heads up!
    Besides that, excellent site!
  • # ï»¿paxlovid https://paxlovid.bid/ paxlovid cost without insurance
    Paxlovid
    Posted @ 2023/10/26 2:47
    paxlovid https://paxlovid.bid/ paxlovid cost without insurance
  • # doxycycline mono https://doxycycline.forum/ buy doxycycline online
    Doxycycline
    Posted @ 2023/11/25 16:36
    doxycycline mono https://doxycycline.forum/ buy doxycycline online
  • # cytotec buy online usa https://cytotec.icu/ cytotec buy online usa
    Cytotec
    Posted @ 2024/01/05 15:07
    cytotec buy online usa https://cytotec.icu/ cytotec buy online usa
  • # sweetie fox cosplay https://sweetiefox.pro/ - sweetie fox cosplay
    SwitieFox
    Posted @ 2024/03/06 23:34
    sweetie fox cosplay https://sweetiefox.pro/ - sweetie fox cosplay
  • # eva elfie new video https://evaelfie.site/ eva elfie full video
    EvaElfie
    Posted @ 2024/03/10 10:39
    eva elfie new video https://evaelfie.site/ eva elfie full video
  • # aviator jogar https://aviatorjogar.online/ - aviator jogar
    BraAvia
    Posted @ 2024/03/13 13:45
    aviator jogar https://aviatorjogar.online/ - aviator jogar
タイトル  
名前  
Url
コメント