如何在VB.net中用分号解析特定元素

时间:2014-09-12 21:47:45

标签: xml vb.net

我尝试解析<geo:lat><geo:long>中的值,并将值存储在两个变量lat和longi中,来自以下XML文件

<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

1 个答案:

答案 0 :(得分: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