はてなダイアリーのRSSを読み込む

うちのブログのRSSがなぜかC#(WPFバインディング等のノード選択)で処理できない

調べたら、はてなのRSSにデフォルト名前空間が使われているせいだった


XPathではデフォルト名前空間は処理できません」
だそうです


つまり


これならOKだけど

<?xml version="1.0" encoding="utf-8"?>

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <channel>
    <title>Faith and Brave</title>
  </channel>
  <item>
    <title>タイトル1</title>
  </item>
  <item>
    <title>タイトル2</title>
  </item>
</rdf:RDF>


これはダメ(現在のはてなRSSはこっち)

<?xml version="1.0" encoding="utf-8"?>

<rdf:RDF xmlns="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <channel>
    <title>Faith and Brave</title>
  </channel>
  <item>
    <title>タイトル1</title>
  </item>
  <item>
    <title>タイトル2</title>
  </item>
</rdf:RDF>
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Program
{
    class Program
    {
        static void Main()
        {
            XmlDocument news = new System.Xml.XmlDocument();

            news.Load("http://d.hatena.ne.jp/faith_and_brave/rss");

            foreach (System.Xml.XmlElement node in news.SelectNodes("//item/title") ) {
                System.Console.WriteLine(node.InnerText);
            }

            news.Save(@"C:/rss.xml");

        }
    }
}


L SharpとかWPFRSS使う予定だったんだが・・・

やれやれ・・・どうしよ




# 2007/09/06 追記

satoshiさんに教えてもらった方法で、はてなダイアリーRSSを読み込むことができました。

ただし、Expression Blendでのバインディングでは↓のような
名前空間のマッピングができませんでした


WPFはてなダイアリーRSSを読みたいなら
Expression Blend使わずにプログラム書きましょう

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument news = new System.Xml.XmlDocument();

            news.Load("http://d.hatena.ne.jp/faith_and_brave/rss");

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(news.NameTable);
            nsmgr.AddNamespace("rss", "http://purl.org/rss/1.0/");
            nsmgr.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
            foreach (System.Xml.XmlElement node in news.SelectNodes("/rdf:RDF/rss:item/rss:title", nsmgr) ) {
                System.Console.WriteLine(node.InnerText);
            }
            news.Save(@"C:/rss.xml");
        }
    }
}