2014-09-12 1 views
0

Я пытаюсь разобрать значения внутри <geo:lat> и <geo:long> и хранить значения двух переменных, широта и Longi, из следующего файла XMLКак разобрать конкретные элементы с запятой в VB.net

<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0"> 
    <channel> 
    <title>Yahoo! Weather - Accrington, GB</title> 
    <link> 
    http://us.rd.yahoo.com/dailynews/rss/weather/Accrington__GB/*http://weather.yahoo.com/forecast/UKXX1241_c.html 
    </link> 
    <description>Yahoo! Weather for Accrington, GB</description> 
    <language>en-us</language> 
    <lastBuildDate>Fri, 12 Sep 2014 4:50 pm BST</lastBuildDate> 
    <ttl>60</ttl> 
    <yweather:location city="Accrington" region="" country="United Kingdom"/> 
    <yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/> 
    <yweather:wind chill="18" direction="140" speed="8.05"/> 
    <yweather:atmosphere humidity="59" visibility="9.99" pressure="1015.92" rising="0"/> 
    <yweather:astronomy sunrise="6:37 am" sunset="7:34 pm"/> 
    <image> 
    <title>Yahoo! Weather</title> 
    <width>142</width> 
    <height>18</height> 
    <link>http://weather.yahoo.com</link> 
    <url> 
    http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif 
    </url> 
    </image> 
    <item> 
    <title>Conditions for Accrington, GB at 4:50 pm BST</title> 
    <geo:lat>53.75</geo:lat> 
    <geo:long>-2.37</geo:long> 
    <link> 
    http://us.rd.yahoo.com/dailynews/rss/weather/Accrington__GB/*http://weather.yahoo.com/forecast/UKXX1241_c.html 
    </link> 
    <pubDate>Fri, 12 Sep 2014 4:50 pm BST</pubDate> 
    <yweather:condition text="Mostly Cloudy" code="28" temp="18" date="Fri, 12 Sep 2014 4:50 pm BST"/> 
    <description> 
    <![CDATA[ 
    <img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br /> <b>Current Conditions:</b><br /> Mostly Cloudy, 18 C<BR /> <BR /><b>Forecast:</b><BR /> Fri - Partly Cloudy. High: 18 Low: 9<br /> Sat - Partly Cloudy. High: 20 Low: 9<br /> Sun - Cloudy. High: 20 Low: 11<br /> Mon - PM Showers. High: 18 Low: 13<br /> Tue - Showers. High: 19 Low: 12<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Accrington__GB/*http://weather.yahoo.com/forecast/UKXX1241_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/> 
    ]]> 
    </description> 
    <yweather:forecast day="Fri" date="12 Sep 2014" low="9" high="18" text="Partly Cloudy" code="29"/> 
    <yweather:forecast day="Sat" date="13 Sep 2014" low="9" high="20" text="Partly Cloudy" code="30"/> 
    <yweather:forecast day="Sun" date="14 Sep 2014" low="11" high="20" text="Cloudy" code="26"/> 
    <yweather:forecast day="Mon" date="15 Sep 2014" low="13" high="18" text="PM Showers" code="39"/> 
    <yweather:forecast day="Tue" date="16 Sep 2014" low="12" high="19" text="Showers" code="11"/> 
    <guid isPermaLink="false">UKXX1241_2014_09_16_7_00_BST</guid> 
    </item> 
    </channel> 
    </rss> 
    <!-- 
    api15.weather.bf1.yahoo.com Fri Sep 12 14:39:57 PDT 2014 
    --> 

В настоящее время у меня есть следующий код, но он дает мне ошибки.

'lat = nodes.Item(0).SelectSingleNode("geo" + "lat").InnerText 
'longi = nodes.Item(0).SelectSingleNode("geo" + "long").InnerText 

Любая помощь с этим было бы весьма признателен

Благодаря Boyercam

+0

Что-то пошло не так в первом предложении. Пожалуйста, отредактируйте его. И скажите, пожалуйста, какие ошибки вы получаете. – honk

ответ

0

Часть до запятой является префиксом пространства имен в вашем случае объявляются в корневом элементе. Вы должны зарегистрировать отображение префикса к-пространства имен URI в XmlNamespaceManager, и использовать зарегистрированный префикс в вашем XPath, например:

Dim doc As XmlDocument = New XmlDocument() 
...... 
...... 
Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable) 
nsManager.AddNamespace("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#") 
lat = nodes.Item(0).SelectSingleNode(".//geo:lat", nsManager).InnerText 
+0

Это работало блестяще, спасибо har07 :) – Boyercam

Смежные вопросы