如何解析用分号特定元素在VB.net
问题描述:
我试图解析<geo:lat>
和<geo:long>
里面的值,并将其存储在两个变量,纬度和隆基,从下面的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
答
分号之前的部分是在你的情况下,在根元素声明命名空间前缀。您需要将前缀至名称空间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 2014-09-12 22:52:18
出了错在您的第一句话。请编辑它。并且请告诉我们你得到了什么样的错误。 – honk 2014-09-12 21:55:00