何となく Blog by Jitta
Microsoft .NET 考

目次

Blog 利用状況
  • 投稿数 - 761
  • 記事 - 18
  • コメント - 37514
  • トラックバック - 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!
  • # Hi there every one, here every person is sharing these kinds of know-how, therefore it's good to read this blog, and I used to visit this website everyday.
    Hi there every one, here every person is sharing t
    Posted @ 2019/11/15 17:10
    Hi there every one, here every person is sharing these kinds of know-how, therefore it's good to read this blog, and I used to visit this website
    everyday.
  • # Hi, I do believe this is an excellent web site. I stumbledupon it ;) I am going to return once again since I saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide others.
    Hi, I do believe this is an excellent web site. I
    Posted @ 2019/12/31 8:53
    Hi, I do believe this is an excellent web site.

    I stumbledupon it ;) I am going to return once again since I saved as
    a favorite it. Money and freedom is the best way to change, may you be rich and
    continue to guide others.
  • # Hi, I do believe this is an excellent web site. I stumbledupon it ;) I am going to return once again since I saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide others.
    Hi, I do believe this is an excellent web site. I
    Posted @ 2019/12/31 8:54
    Hi, I do believe this is an excellent web site.

    I stumbledupon it ;) I am going to return once again since I saved as
    a favorite it. Money and freedom is the best way to change, may you be rich and
    continue to guide others.
  • # Hi, I do believe this is an excellent web site. I stumbledupon it ;) I am going to return once again since I saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide others.
    Hi, I do believe this is an excellent web site. I
    Posted @ 2019/12/31 8:55
    Hi, I do believe this is an excellent web site.

    I stumbledupon it ;) I am going to return once again since I saved as
    a favorite it. Money and freedom is the best way to change, may you be rich and
    continue to guide others.
  • # Hi, I do believe this is an excellent web site. I stumbledupon it ;) I am going to return once again since I saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide others.
    Hi, I do believe this is an excellent web site. I
    Posted @ 2019/12/31 8:56
    Hi, I do believe this is an excellent web site.

    I stumbledupon it ;) I am going to return once again since I saved as
    a favorite it. Money and freedom is the best way to change, may you be rich and
    continue to guide others.
  • # Incredible points. Sound arguments. Keep up the great spirit.
    Incredible points. Sound arguments. Keep up the g
    Posted @ 2020/01/01 10:41
    Incredible points. Sound arguments. Keep up the great spirit.
  • # Incredible points. Sound arguments. Keep up the great spirit.
    Incredible points. Sound arguments. Keep up the g
    Posted @ 2020/01/01 10:42
    Incredible points. Sound arguments. Keep up the great spirit.
  • # Incredible points. Sound arguments. Keep up the great spirit.
    Incredible points. Sound arguments. Keep up the g
    Posted @ 2020/01/01 10:43
    Incredible points. Sound arguments. Keep up the great spirit.
  • # Incredible points. Sound arguments. Keep up the great spirit.
    Incredible points. Sound arguments. Keep up the g
    Posted @ 2020/01/01 10:44
    Incredible points. Sound arguments. Keep up the great spirit.
  • # 3 He was too Training participants were 2
    meet-old-ladies
    Posted @ 2020/05/23 2:52
    http://ievakelpsaite.com/14513-2/ where to meet european singles
  • # 5 Article 1 of Despite never attending 6
    meet-old-ladies
    Posted @ 2020/05/23 12:09
    http://pagor-pw.pl/2622-2/ where to meet canadian singles in vancouver free
タイトル  
名前  
Url
コメント