0

私がやりたいことは、rss から yahoo 天気 xml にアクセスし、特に yweather:condition タグからデータを取得することです。で試しました

xdoc.Load("http://xml.weather.yahoo.com/forecastrss?p=MKXX0001&u=c");
XmlNode xNode = xdoc.DocumentElement.SelectSingleNode("yweather:condition"); 

しかし、成功しませんでした。yahoo weather から xml にアクセスして、そこにあるすべての属性を取得するにはどうすればよいですか? また、すべての属性をローカルの xml ファイルに保存するにはどうすればよいですか?

4

2 に答える 2

0

XPath を学習して、xml のすべての特定の要素を選択する方法を理解します。XmlNamespaceManagerYahoo 天気 xml には名前空間があるため、メソッドの 2 番目の引数として必要になりますSelectSingleNode<yweather:condition>次の例は、 elementからすべての属性を取得する方法を示しています。

var xdoc = new XmlDocument();
xdoc.Load("http://xml.weather.yahoo.com/forecastrss?p=MKXX0001&u=c");
var nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
var _attributes = xdoc.SelectSingleNode("/rss/channel/item/yweather:condition", nsmgr).Attributes;
foreach (XmlAttribute attr in _attributes)
{
    Console.WriteLine("Attribute: {0}, Value: {1}", attr.Name, attr.Value);
}
于 2014-02-08T10:33:38.153 に答える
0

XPath が間違っています

期待される XPath は

/rss/channel/item/yweather:condition

その他、XPath にはプレフィックスが含まれているため、namespacemanager を指定する必要があります。

あなたのコードは

XmlDocument xdoc = new XmlDocument();
xdoc.Load("http://xml.weather.yahoo.com/forecastrss?p=MKXX0001&u=c");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode xNode = xdoc.DocumentElement.SelectSingleNode("/rss/channel/item/yweather:condition", nsmgr);
于 2014-02-08T10:03:15.347 に答える